Skip to main content

ssi_data_integrity_core/signing/
mod.rs

1use ssi_claims_core::{ProofValidationError, SignatureError};
2use ssi_crypto::{
3    algorithm::{self, Algorithm, ES256OrES384, SignatureAlgorithmType},
4    AlgorithmInstance,
5};
6
7mod jws;
8pub use jws::*;
9
10mod multibase;
11pub use multibase::*;
12use ssi_verification_methods::Multikey;
13
14pub enum AlgorithmSelectionError {
15    MissingAlgorithm,
16    InvalidKey,
17}
18
19impl From<AlgorithmSelectionError> for SignatureError {
20    fn from(value: AlgorithmSelectionError) -> Self {
21        match value {
22            AlgorithmSelectionError::MissingAlgorithm => Self::MissingAlgorithm,
23            AlgorithmSelectionError::InvalidKey => Self::InvalidPublicKey,
24        }
25    }
26}
27
28impl From<AlgorithmSelectionError> for ProofValidationError {
29    fn from(value: AlgorithmSelectionError) -> Self {
30        match value {
31            AlgorithmSelectionError::MissingAlgorithm => Self::MissingAlgorithm,
32            AlgorithmSelectionError::InvalidKey => Self::InvalidKey,
33        }
34    }
35}
36
37pub trait AlgorithmSelection<M, O>: SignatureAlgorithmType {
38    fn select_algorithm(
39        verification_method: &M,
40        options: &O,
41    ) -> Result<Self::Instance, AlgorithmSelectionError>;
42}
43
44impl<M: ssi_verification_methods::JwkVerificationMethod, O> AlgorithmSelection<M, O> for Algorithm {
45    fn select_algorithm(
46        verification_method: &M,
47        _options: &O,
48    ) -> Result<AlgorithmInstance, AlgorithmSelectionError> {
49        verification_method
50            .to_jwk()
51            .get_algorithm()
52            .ok_or(AlgorithmSelectionError::MissingAlgorithm)
53            .map(Into::into)
54    }
55}
56
57impl<M: ssi_verification_methods::JwkVerificationMethod, O> AlgorithmSelection<M, O>
58    for ssi_jwk::Algorithm
59{
60    fn select_algorithm(
61        verification_method: &M,
62        _options: &O,
63    ) -> Result<Self, AlgorithmSelectionError> {
64        verification_method
65            .to_jwk()
66            .get_algorithm()
67            .ok_or(AlgorithmSelectionError::MissingAlgorithm)
68    }
69}
70
71impl<M, O> AlgorithmSelection<M, O> for algorithm::ES256KR {
72    fn select_algorithm(
73        _verification_method: &M,
74        _options: &O,
75    ) -> Result<Self, AlgorithmSelectionError> {
76        Ok(Self)
77    }
78}
79
80impl<M, O> AlgorithmSelection<M, O> for algorithm::ES256K {
81    fn select_algorithm(
82        _verification_method: &M,
83        _options: &O,
84    ) -> Result<Self, AlgorithmSelectionError> {
85        Ok(Self)
86    }
87}
88
89impl<M, O> AlgorithmSelection<M, O> for algorithm::ES256 {
90    fn select_algorithm(
91        _verification_method: &M,
92        _options: &O,
93    ) -> Result<Self, AlgorithmSelectionError> {
94        Ok(Self)
95    }
96}
97
98impl<M, O> AlgorithmSelection<M, O> for algorithm::EdDSA {
99    fn select_algorithm(
100        _verification_method: &M,
101        _options: &O,
102    ) -> Result<Self, AlgorithmSelectionError> {
103        Ok(Self)
104    }
105}
106
107impl<M, O> AlgorithmSelection<M, O> for algorithm::EdBlake2b {
108    fn select_algorithm(
109        _verification_method: &M,
110        _options: &O,
111    ) -> Result<Self, AlgorithmSelectionError> {
112        Ok(Self)
113    }
114}
115
116impl<M, O> AlgorithmSelection<M, O> for algorithm::ESBlake2b {
117    fn select_algorithm(
118        _verification_method: &M,
119        _options: &O,
120    ) -> Result<Self, AlgorithmSelectionError> {
121        Ok(Self)
122    }
123}
124
125impl<O> AlgorithmSelection<Multikey, O> for ES256OrES384 {
126    fn select_algorithm(
127        verification_method: &Multikey,
128        _options: &O,
129    ) -> Result<Self, AlgorithmSelectionError> {
130        match verification_method
131            .public_key
132            .decode()
133            .map_err(|_| AlgorithmSelectionError::InvalidKey)?
134        {
135            #[cfg(feature = "secp256r1")]
136            ssi_verification_methods::multikey::DecodedMultikey::P256(_) => Ok(Self::ES256),
137            #[cfg(feature = "secp384r1")]
138            ssi_verification_methods::multikey::DecodedMultikey::P384(_) => Ok(Self::ES384),
139            _ => Err(AlgorithmSelectionError::InvalidKey),
140        }
141    }
142}
143
144pub trait AlterSignature {
145    fn alter(&mut self);
146}