forked from spruceid/ssi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for COSE for VCs. (spruceid#593)
* Update MSRV.
- Loading branch information
1 parent
0b26eac
commit 099293a
Showing
36 changed files
with
2,527 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "ssi-cose" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Spruce Systems, Inc."] | ||
license = "Apache-2.0" | ||
description = "CBOR Object Signing and Encryption for the `ssi` library." | ||
repository = "https://github.com/spruceid/ssi/" | ||
documentation = "https://docs.rs/ssi-cose/" | ||
|
||
[features] | ||
default = ["secp256r1"] | ||
ed25519 = ["ssi-crypto/ed25519"] | ||
secp256k1 = ["ssi-crypto/secp256k1"] | ||
secp256r1 = ["ssi-crypto/secp256r1"] | ||
secp384r1 = ["ssi-crypto/secp384r1"] | ||
|
||
[dependencies] | ||
ssi-crypto.workspace = true | ||
ssi-claims-core.workspace = true | ||
thiserror.workspace = true | ||
ciborium.workspace = true | ||
coset = { version = "0.3.8", features = ["std"] } | ||
serde = { workspace = true, features = ["derive"] } | ||
|
||
[dev-dependencies] | ||
serde_json.workspace = true | ||
hex.workspace = true | ||
async-std = { workspace = true, features = ["attributes"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use std::borrow::Cow; | ||
|
||
use coset::{ | ||
iana::{self, EnumI64}, | ||
Algorithm, CoseKey, KeyType, | ||
}; | ||
use ssi_crypto::AlgorithmInstance; | ||
|
||
use crate::key::{CoseKeyDecode, EC2_CRV}; | ||
|
||
/// Converts a COSE algorithm into an SSI algorithm instance. | ||
pub fn instantiate_algorithm(algorithm: &Algorithm) -> Option<AlgorithmInstance> { | ||
match algorithm { | ||
Algorithm::Assigned(iana::Algorithm::PS256) => Some(AlgorithmInstance::PS256), | ||
Algorithm::Assigned(iana::Algorithm::PS384) => Some(AlgorithmInstance::PS384), | ||
Algorithm::Assigned(iana::Algorithm::PS512) => Some(AlgorithmInstance::PS512), | ||
Algorithm::Assigned(iana::Algorithm::EdDSA) => Some(AlgorithmInstance::EdDSA), | ||
Algorithm::Assigned(iana::Algorithm::ES256K) => Some(AlgorithmInstance::ES256K), | ||
Algorithm::Assigned(iana::Algorithm::ES256) => Some(AlgorithmInstance::ES256), | ||
Algorithm::Assigned(iana::Algorithm::ES384) => Some(AlgorithmInstance::ES384), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Computes a proper display name for the give COSE algorithm. | ||
pub fn algorithm_name(algorithm: &Algorithm) -> String { | ||
match algorithm { | ||
Algorithm::Assigned(iana::Algorithm::PS256) => "PS256".to_owned(), | ||
Algorithm::Assigned(iana::Algorithm::PS384) => "PS384".to_owned(), | ||
Algorithm::Assigned(iana::Algorithm::PS512) => "PS512".to_owned(), | ||
Algorithm::Assigned(iana::Algorithm::EdDSA) => "EdDSA".to_owned(), | ||
Algorithm::Assigned(iana::Algorithm::ES256K) => "ES256K".to_owned(), | ||
Algorithm::Assigned(iana::Algorithm::ES256) => "ES256".to_owned(), | ||
Algorithm::Assigned(iana::Algorithm::ES384) => "ES384".to_owned(), | ||
Algorithm::Assigned(i) => format!("assigned({})", i.to_i64()), | ||
Algorithm::PrivateUse(i) => format!("private_use({i})"), | ||
Algorithm::Text(text) => text.to_owned(), | ||
} | ||
} | ||
|
||
/// Returns the preferred signature algorithm for the give COSE key. | ||
pub fn preferred_algorithm(key: &CoseKey) -> Option<Cow<Algorithm>> { | ||
key.alg | ||
.as_ref() | ||
.map(Cow::Borrowed) | ||
.or_else(|| match key.kty { | ||
KeyType::Assigned(iana::KeyType::RSA) => { | ||
Some(Cow::Owned(Algorithm::Assigned(iana::Algorithm::PS256))) | ||
} | ||
KeyType::Assigned(iana::KeyType::OKP) => { | ||
let crv = key | ||
.parse_required_param(&EC2_CRV, |v| { | ||
v.as_integer().and_then(|i| i64::try_from(i).ok()) | ||
}) | ||
.ok()?; | ||
|
||
match iana::EllipticCurve::from_i64(crv)? { | ||
iana::EllipticCurve::Ed25519 => { | ||
Some(Cow::Owned(Algorithm::Assigned(iana::Algorithm::EdDSA))) | ||
} | ||
_ => None, | ||
} | ||
} | ||
KeyType::Assigned(iana::KeyType::EC2) => { | ||
let crv = key | ||
.parse_required_param(&EC2_CRV, |v| { | ||
v.as_integer().and_then(|i| i64::try_from(i).ok()) | ||
}) | ||
.ok()?; | ||
|
||
match iana::EllipticCurve::from_i64(crv)? { | ||
iana::EllipticCurve::Secp256k1 => { | ||
Some(Cow::Owned(Algorithm::Assigned(iana::Algorithm::ES256K))) | ||
} | ||
iana::EllipticCurve::P_256 => { | ||
Some(Cow::Owned(Algorithm::Assigned(iana::Algorithm::ES256))) | ||
} | ||
iana::EllipticCurve::P_384 => { | ||
Some(Cow::Owned(Algorithm::Assigned(iana::Algorithm::ES384))) | ||
} | ||
_ => None, | ||
} | ||
} | ||
_ => None, | ||
}) | ||
} |
Oops, something went wrong.