cawg_identity/claim_aggregation/ica_signature_verifier.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
use async_trait::async_trait;
use coset::{CoseSign1, RegisteredLabelWithPrivate, TaggedCborSerializable};
use crate::{
claim_aggregation::{
w3c_vc::{
did::Did,
did_web,
jwk::{Algorithm, Jwk, JwkError, Params},
},
IcaCredential, IcaValidationError,
},
SignatureVerifier, SignerPayload, ValidationError,
};
/// An implementation of [`SignatureVerifier`] that supports Identity Claims
/// Aggregation Credentials (a specific grammar of W3C Verifiable Credentials)
/// as specified in [§8.1, Identity claims aggregation] and secured by COSE as
/// specified in [§3.3.1 Securing JSON-LD Verifiable Credentials with COSE] of
/// _Securing Verifiable Credentials using JOSE and COSE._
///
/// [`SignatureVerifier`]: crate::SignatureVerifier
/// [§8.1, Identity claims aggregation]: https://creator-assertions.github.io/identity/1.1-draft/#_identity_claims_aggregation
/// [§3.3.1 Securing JSON-LD Verifiable Credentials with COSE]: https://w3c.github.io/vc-jose-cose/#securing-vcs-with-cose
pub struct IcaSignatureVerifier {}
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl SignatureVerifier for IcaSignatureVerifier {
type Error = IcaValidationError;
type Output = IcaCredential;
async fn check_signature(
&self,
_signer_payload: &SignerPayload,
signature: &[u8],
) -> Result<Self::Output, ValidationError<Self::Error>> {
// The signature should be a `CoseSign1` object.
let sign1 = CoseSign1::from_tagged_slice(signature)?;
// Identify the signature
let _ssi_alg = if let Some(ref alg) = sign1.protected.header.alg {
match alg {
// TEMPORARY: Require EdDSA algorithm.
RegisteredLabelWithPrivate::Assigned(coset::iana::Algorithm::EdDSA) => {
Algorithm::EdDsa
}
_ => {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedSignatureType(format!("{alg:?}")),
));
}
}
} else {
return Err(ValidationError::SignatureError(
IcaValidationError::SignatureTypeMissing,
));
};
if let Some(ref cty) = sign1.protected.header.content_type {
match cty {
coset::ContentType::Text(ref cty) => {
if cty != "application/vc" {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedContentType(format!("{cty:?}")),
));
}
}
_ => {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedContentType(format!("{cty:?}")),
));
}
}
} else {
return Err(ValidationError::SignatureError(
IcaValidationError::ContentTypeMissing,
));
}
// Interpret the unprotected payload, which should be the raw VC.
let Some(ref payload_bytes) = sign1.payload else {
return Err(ValidationError::SignatureError(
IcaValidationError::CredentialPayloadMissing,
));
};
let ica_credential: IcaCredential = serde_json::from_slice(payload_bytes)?;
// Discover public key for issuer DID and validate signature.
// TEMPORARY version supports did:jwk and did:web only.
let issuer_id = Did::new(&ica_credential.issuer)?;
let (primary_did, _fragment) = issuer_id.split_fragment();
let jwk = match primary_did.method_name() {
"jwk" => {
let jwk = primary_did.method_specific_id();
let jwk =
multibase::Base::decode(&multibase::Base::Base64Url, jwk).map_err(|e| {
ValidationError::SignatureError(IcaValidationError::UnsupportedIssuerDid(
e.to_string(),
))
})?;
let jwk: Jwk = serde_json::from_slice(&jwk).map_err(|e| {
ValidationError::SignatureError(IcaValidationError::UnsupportedIssuerDid(
e.to_string(),
))
})?;
jwk
}
"web" => {
let did_doc = did_web::resolve(&primary_did).await?;
let Some(vm1) = did_doc.verification_relationships.assertion_method.first() else {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedIssuerDid(
"DID document doesn't contain an assertion_method entry".to_string(),
),
));
};
let super::w3c_vc::did_doc::ValueOrReference::Value(vm1) = vm1 else {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedIssuerDid(
"DID document's assertion_method is not a value".to_string(),
),
));
};
let Some(jwk_prop) = vm1.properties.get("publicKeyJwk") else {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedIssuerDid(
"DID document's assertion_method doesn't contain a publicKeyJwk entry"
.to_string(),
),
));
};
dbg!(&jwk_prop);
// OMG SO HACKY!
let Ok(jwk_json) = serde_json::to_string_pretty(jwk_prop) else {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedIssuerDid(
"couldn't re-serialize JWK".to_string(),
),
));
};
let Ok(jwk) = serde_json::from_str(&jwk_json) else {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedIssuerDid(
"couldn't re-serialize JWK".to_string(),
),
));
};
jwk
}
x => {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedIssuerDid(format!("unsupported DID method {x}")),
));
}
};
// TEMPORARY: only support ED25519.
let Params::Okp(ref okp) = jwk.params;
if okp.curve != "Ed25519" {
return Err(ValidationError::SignatureError(
IcaValidationError::UnsupportedIssuerDid(format!(
"unsupported OKP curve {}",
okp.curve
)),
));
}
// Check the signature, which needs to have the same `aad` provided, by
// providing a closure that can do the verify operation.
sign1
.verify_signature(b"", |sig, data| {
use ed25519_dalek::Verifier;
let public_key = ed25519_dalek::VerifyingKey::try_from(okp)?;
let signature: ed25519_dalek::Signature = sig.try_into().map_err(JwkError::from)?;
public_key.verify(data, &signature).map_err(JwkError::from)
})
.map_err(|_e| ValidationError::InvalidSignature)?;
// Enforce [§8.1.1.4. Validity].
//
// [§8.1.1.4. Validity]: https://creator-assertions.github.io/identity/1.1-draft/#vc-property-validFrom
let Some(_valid_from) = ica_credential.valid_from else {
return Err(ValidationError::SignatureError(
IcaValidationError::MissingValidFromDate,
));
};
// TO DO: Enforce signer_payload matches what was stated outside the signature.
// TO DO: Enforce validity window as compared to sig time (or now if no TSA
// time).
// TO DO: Verify that signer_payload is same as c2paAsset.
Ok(ica_credential)
}
}