Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JWT VC support for RSA keys; enable ZKP tests #24

Merged
merged 4 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ serde_json = "1.0"
pest = "2.1"
pest_derive = "2.1"
derive_builder = "0.9"
base64 = "0.12"
jsonwebtoken = "7"
21 changes: 17 additions & 4 deletions src/bin/ssi-vc-test/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@
"generator": "../ssi/target/debug/ssi-vc-test generate",
"presentationGenerator": "../ssi/target/debug/ssi-vc-test generate-presentation",
"generatorOptions": "",
"sectionsNotSupported": [
"jwt",
"zkp"
]
"sectionsNotSupported": [],
"jwt": {
"rs256PrivateKeyJwk": {
"kty": "RSA",
"n": "ALG1_NjU1eiMpcQoezH1eIZcr2p4gmo7j_exsjqkr05qBHQTn5Rb2dOpEf_Q-nLbeORZOobJH52oNhhuvTQC1R9qOEQkerUIi0aSnDEbQ2BP7YRJnwbw85v1VZDiGH8bviKbqPSf0uVwzQ43M8dr_t6A4efgiB5FOOobxXgr6VEvi_EyUm4F9JI1ZP5uHiG_cIet67Zg-lM_ygbQqOZpkeExLXC2SmHajKd8Aozfk5YB4s_-KFV1mx1HnwhHvKxZ19YkJ6Qx1OPf0ml0KLndYwmnsRZ9uZ_gGvH_G3OVZvuUnkcInR2uhK7xIU739Xc-hqDT6dokb5vsCdYMcFiRGD0=",
"e": "AQAB",
"d": "Eym3sT4KLwBzo5pl5nY83-hAti92iLQRizkrKe22RbNi9Y1kKOBatdtGaJqFVztZZu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Ee1WUWtaKSNhbNh0PUGGXlXHGlhkS8jI1ot0e-scrHAuACE567YQ4VurpNorPKtZ5UENXIn74DEmt4l5m6902VF3X5Wo=",
"alg": "RS256",
"kid": "rsa2048-2020-08-25"
},
"aud": "did:example:0xcafe"
}
}
181 changes: 138 additions & 43 deletions src/bin/ssi-vc-test/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use ssi::vc::Contexts;
use ssi::jwk::JWTKeys;
use ssi::vc::Context;
use ssi::vc::Credential;
use ssi::vc::OneOrMany;
use ssi::vc::Presentation;

fn usage() {
Expand All @@ -11,22 +13,56 @@ fn generate(data: String) -> String {
if !doc.type_.contains(&"VerifiableCredential".to_string()) {
panic!("Missing type VerifiableCredential");
}
if doc.issuer.is_none() {
panic!("Missing issuer");
}
if doc.issuance_date.is_none() {
panic!("Missing issuance date");
}
if doc.proof.is_none() {
panic!("Missing proof");
}

// work around https://github.com/w3c/vc-test-suite/issues/96
if doc.type_.len() > 1 {
if let Contexts::Many(ref context) = doc.context {
if context.len() == 1 {
panic!("If there are multiple types, there should be multiple contexts.");
}
let is_zkp = match &doc.proof {
Some(proofs) => proofs.any(|proof| proof.type_.contains(&"CLSignature2019".to_string())),
_ => false,
};
if is_zkp {
if doc.credential_schema.is_none() {
panic!("Missing credential schema for ZKP");
}
}

// @TODO: sign/verify
return serde_json::to_string_pretty(&doc).unwrap();
// work around https://github.com/w3c/vc-test-suite/issues/96
let contexts: &OneOrMany<Context> = &doc.context.clone().into();
if doc.type_.len() > 1 && contexts.len() <= 1 {
panic!("If there are multiple types, there should be multiple contexts.");
}

// work around https://github.com/w3c/vc-test-suite/issues/97
if contexts.len() > 1 && doc.type_.len() <= 1 {
panic!("If there are multiple contexts, there should be multiple types.");
}

serde_json::to_string_pretty(&doc).unwrap()
}

fn generate_jwt(data: &String, keys: &JWTKeys, aud: &String, sign: bool) -> String {
let vc: Credential = serde_json::from_str(data).unwrap();
if sign {
vc.encode_sign_jwt(keys, aud).unwrap()
} else {
vc.encode_jwt_unsigned(aud).unwrap()
}
}

fn generate_presentation(data: String) -> String {
let doc: Presentation = serde_json::from_str(&data).unwrap();
fn decode_jwt_unsigned(data: &String) -> String {
let vc = Credential::from_jwt_unsigned(data).unwrap();
serde_json::to_string_pretty(&vc).unwrap()
}

fn generate_presentation(data: &String) -> String {
let doc: Presentation = serde_json::from_str(data).unwrap();
if !doc.type_.contains(&"VerifiablePresentation".to_string()) {
panic!("Missing type VerifiablePresentation");
}
Expand All @@ -36,57 +72,116 @@ fn generate_presentation(data: String) -> String {
panic!("Missing proof");
}

// @TODO: sign/verify
let response = serde_json::to_string_pretty(&doc).unwrap();
return response;
serde_json::to_string_pretty(&doc).unwrap()
}

fn read_json(filename: &String) -> String {
let mut file = match std::fs::File::open(filename) {
Err(err) => panic!("Unable to open {}: {}", filename, err),
Ok(file) => file,
};
let mut data = String::new();
fn generate_jwt_presentation(data: &String, keys: &JWTKeys, aud: &String) -> String {
let vp: Presentation = serde_json::from_str(data).unwrap();
vp.encode_sign_jwt(keys, aud).unwrap()
}

fn read_file(filename: &String) -> String {
let mut file = std::fs::File::open(filename).unwrap();
let mut data = String::new();
use std::io::Read;
let data = match file.read_to_string(&mut data) {
Err(err) => panic!("Unable to read {}: {}", filename, err),
Ok(_) => data,
};
// TODO: parse JSON
file.read_to_string(&mut data).unwrap();
data
}

fn write_json(data: String) {
fn write_out(data: String) {
use std::io::Write;
let stdout = std::io::stdout();
let mut handle = stdout.lock();
match handle.write_all(data.as_bytes()) {
Err(err) => panic!("Unable to write output: {}", err),
Ok(_) => {}
}
stdout.lock().write_all(data.as_bytes()).unwrap();
}

fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() != 3 {
let args = std::env::args();
let mut cmd: Option<String> = None;
let mut filename: Option<String> = None;
let mut jwt_keys: Option<JWTKeys> = None;
let mut jwt_aud: Option<String> = None;
let mut jwt_no_jws = false;
let mut jwt_presentation = false;
let mut jwt_decode = false;
let mut args_iter = args.into_iter();
let _bin = args_iter.next().unwrap();
loop {
match args_iter.next() {
Some(arg) => match (arg.starts_with("--"), arg.as_ref()) {
(true, "--jwt") => match args_iter.next() {
Some(jwt_b64) => {
let jwt_json = base64::decode(jwt_b64).unwrap();
jwt_keys = Option::Some(serde_json::from_slice(&jwt_json).unwrap());
}
None => {}
},
(true, "--jwt-aud") => jwt_aud = args_iter.next(),
(true, "--jwt-no-jws") => jwt_no_jws = true,
(true, "--jwt-presentation") => jwt_presentation = true,
(true, "--jwt-decode") => jwt_decode = true,
(true, _) => panic!("Unexpected option '{}'", arg),
(false, _) => {
if cmd == None {
cmd = Option::Some(arg);
} else if filename == None {
filename = Option::Some(arg);
} else {
panic!("Unexpected argument '{}'", arg);
}
}
},
None => break,
}
}
if cmd == None || filename == None {
return usage();
}
let cmd = &args[1];
let filename = &args[2];
match &cmd[..] {
// work around https://github.com/w3c/vc-test-suite/issues/98
if filename.as_ref().unwrap().contains("example-015-zkp") {
jwt_keys = None;
jwt_aud = None;
jwt_decode = false;
}

let cmd_str = cmd.unwrap();
match cmd_str.as_ref() {
"generate" => {
let data: String = read_json(&filename);
let output: String = generate(data);
write_json(output);
let data: String = read_file(&filename.unwrap());
let output: String;
if jwt_decode {
output = decode_jwt_unsigned(&data);
} else if let Some(keys) = jwt_keys {
if let Some(aud) = jwt_aud {
output = generate_jwt(&data, &keys, &aud, !jwt_no_jws);
} else {
panic!("Expected --jwt-aud with --jwt");
}
} else {
output = generate(data);
}
write_out(output);
}
"generate-presentation" => {
let data: String = read_json(&filename);
let output: String = generate_presentation(data);
write_json(output);
let data: String = read_file(&filename.unwrap());
let output: String;
if let Some(keys) = jwt_keys {
if let Some(aud) = jwt_aud {
if !jwt_presentation {
// vc-test-suite says this is optional, but it seems
// to be always used.
panic!("Expected --jwt-presentation with --jwt");
}
output = generate_jwt_presentation(&data, &keys, &aud);
} else {
panic!("Expected --jwt-aud with --jwt");
}
} else {
output = generate_presentation(&data);
}
write_out(output);
}
_ => {
eprintln!("Unexpected command '{}'", cmd);
eprintln!("Unexpected command '{}'", cmd_str);
std::process::exit(1);
}
}
Expand Down
Loading