-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vical CoseSign1 verification added; code cleanup
- Loading branch information
1 parent
2d93f02
commit 55b6252
Showing
5 changed files
with
68 additions
and
89 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
69 changes: 29 additions & 40 deletions
69
src/definitions/namespaces/org_iso_18013_5_1_vical/vical_cose_sign1.rs
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 |
---|---|---|
@@ -1,67 +1,56 @@ | ||
use crate::definitions::namespaces::org_iso_18013_5_1_vical::OrgIso1901351Vical; | ||
use coset::{iana, CoseSign1}; | ||
use coset::{CborSerializable, CoseSign1}; | ||
use p256::ecdsa::{Signature}; | ||
use signature::{SignatureEncoding, Signer}; | ||
use crate::cose::SignatureAlgorithm; | ||
use signature::{Signer, Verifier}; | ||
use crate::cose::{SignatureAlgorithm}; | ||
use crate::definitions::traits::ToCbor; | ||
|
||
pub fn sign_vical<S, Sig>(vical: OrgIso1901351Vical, signer: &S) -> CoseSign1 | ||
pub fn sign_vical<S>(vical: OrgIso1901351Vical, signer: &S) -> CoseSign1 | ||
where | ||
S: Signer<Sig> + SignatureAlgorithm, | ||
Sig: SignatureEncoding | ||
S: Signer<Signature> + SignatureAlgorithm, | ||
{ | ||
let aad = b""; | ||
let protected = coset::HeaderBuilder::new() | ||
.algorithm(iana::Algorithm::ES256) | ||
.algorithm(signer.algorithm()) | ||
.key_id(b"11".to_vec()) | ||
.build(); | ||
|
||
let cose_sign = coset::CoseSign1Builder::new() | ||
coset::CoseSign1Builder::new() | ||
.protected(protected) | ||
.payload(vical.to_cbor_bytes().unwrap()) | ||
.create_signature(aad, |pt| signer.sign(pt).to_vec()) // closure to do sign operation | ||
.build(); | ||
cose_sign | ||
.create_signature(aad, |pt| signer.sign(pt).to_vec()) | ||
.build() | ||
} | ||
pub fn verify_vical<V>(sign_data: Vec<u8>, verifier: &V) -> Result<(), signature::Error> | ||
where | ||
V: Verifier<Signature> + SignatureAlgorithm, | ||
{ | ||
let aad = b""; | ||
let cose_sign1 = CoseSign1::from_slice(&sign_data).unwrap(); | ||
cose_sign1.verify_signature(aad, |sig, data| verifier.verify(data, &Signature::from_slice(sig).unwrap())) | ||
} | ||
#[cfg(test)] | ||
mod tests { | ||
use coset::CborSerializable; | ||
use hex::FromHex; | ||
use p256::ecdsa::{SigningKey}; | ||
use p256::ecdsa::{SigningKey, VerifyingKey}; | ||
use p256::SecretKey; | ||
use crate::definitions::traits::FromJson; | ||
use super::*; | ||
static COSE_KEY: &str = include_str!("../../../../test/definitions/cose/sign1/secret_key"); | ||
static JSON_VICAL: &str = include_str!("../../../../test/definitions/namespaces/org_iso_18013_5_1_vical/vical.json"); | ||
#[test] | ||
fn test_sign_vical() { | ||
let key = Vec::<u8>::from_hex(COSE_KEY).unwrap(); | ||
let signer: SigningKey = SecretKey::from_slice(&key).unwrap().into(); | ||
|
||
let json = serde_json::json!({ | ||
"version": "1.0.0", | ||
"vical_provider": "Spruce", | ||
"date": "2024-12-31T12:00:00Z", | ||
"vical_issue_id": 1, | ||
"next_update": "2022-03-21T13:30:00Z", | ||
"certificate_infos": [ | ||
{ | ||
"certificate": "57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3", | ||
"serial_number": "57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3", | ||
"ski": "57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3", | ||
"doc_type": ["somedoc"], | ||
"certificate_profile": ["profile"], | ||
"extensions": {"extension_name": "57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3"}, | ||
} | ||
] | ||
}); | ||
let vical = OrgIso1901351Vical::from_json(&json).unwrap(); | ||
let sign = sign_vical::<SigningKey, Signature>(vical, &signer); | ||
|
||
let key_bytes = Vec::<u8>::from_hex(COSE_KEY).unwrap(); | ||
let signer: SigningKey = SecretKey::from_slice(&key_bytes).unwrap().into(); | ||
let verifier = VerifyingKey::from(&signer); | ||
let json_vical: serde_json::Value = serde_json::from_str(JSON_VICAL).unwrap(); | ||
let vical = OrgIso1901351Vical::from_json(&json_vical).unwrap(); | ||
let sign = sign_vical::<SigningKey>(vical, &signer); | ||
// println!("{:#?}", hex::encode(sign.to_vec().unwrap())); | ||
let sign_data = sign.to_vec().unwrap(); | ||
let sign1 = coset::CoseSign1::from_slice(&sign_data).unwrap(); | ||
// let result = sign1.verify_signature(b"", |sig, data| verifier.verify(sig, data)); | ||
// println!("Signature verified: {:?}.", result); | ||
// assert!(result.is_ok()); | ||
// println!("{:#?}", hex::encode(&sign.to_vec().unwrap())); | ||
let result = verify_vical::<VerifyingKey>(sign_data, &verifier); | ||
println!("Signature verified: {:?}.", result); | ||
assert!(result.is_ok()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
test/definitions/namespaces/org_iso_18013_5_1_vical/vical.json
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,23 @@ | ||
{ | ||
"version": "1.0.0", | ||
"vical_provider": "Spruce", | ||
"date": "2024-12-31T12:00:00Z", | ||
"vical_issue_id": 1, | ||
"next_update": "2022-03-21T13:30:00Z", | ||
"certificate_infos": [ | ||
{ | ||
"certificate": "57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3", | ||
"serial_number": "57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3", | ||
"ski": "57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3", | ||
"doc_type": [ | ||
"somedoc" | ||
], | ||
"certificate_profile": [ | ||
"profile" | ||
], | ||
"extensions": { | ||
"extension_name": "57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3" | ||
} | ||
} | ||
] | ||
} |