|
1 | 1 | mod nfc; |
2 | 2 |
|
3 | 3 | use std::fs::File; |
4 | | -use std::io::{stdin, stdout, Read, Write}; |
| 4 | +use std::io::{stderr, stdin, stdout, Read, Write}; |
5 | 5 | use std::path::PathBuf; |
6 | 6 | use std::process::exit; |
7 | 7 |
|
8 | 8 | use clap::{ArgEnum, Parser, Subcommand}; |
9 | 9 | use dialoguer::Password; |
| 10 | +use jpki::ap::jpki as jpki_ap; |
10 | 11 | use jpki::nfc::apdu::{Command, Handler, Response}; |
| 12 | +use tracing::{error, info}; |
| 13 | +use tracing_subscriber::EnvFilter; |
11 | 14 |
|
12 | 15 | use crate::nfc::{Context, Initiator, Target}; |
13 | 16 |
|
@@ -49,13 +52,15 @@ enum CertType { |
49 | 52 | AuthCA, |
50 | 53 | } |
51 | 54 |
|
52 | | -impl Into<jpki::ap::jpki::CertType> for CertType { |
53 | | - fn into(self) -> jpki::ap::jpki::CertType { |
| 55 | +impl Into<jpki_ap::CertType> for CertType { |
| 56 | + fn into(self) -> jpki_ap::CertType { |
| 57 | + use jpki_ap::CertType::*; |
| 58 | + |
54 | 59 | match self { |
55 | | - Self::Sign => jpki::ap::jpki::CertType::Sign, |
56 | | - Self::SignCA => jpki::ap::jpki::CertType::SignCA, |
57 | | - Self::Auth => jpki::ap::jpki::CertType::Auth, |
58 | | - Self::AuthCA => jpki::ap::jpki::CertType::AuthCA, |
| 60 | + Self::Sign => Sign, |
| 61 | + Self::SignCA => SignCA, |
| 62 | + Self::Auth => Auth, |
| 63 | + Self::AuthCA => AuthCA, |
59 | 64 | } |
60 | 65 | } |
61 | 66 | } |
@@ -107,20 +112,20 @@ fn read_all<R: Read>(mut r: R) -> Result<Vec<u8>> { |
107 | 112 | Ok(buffer) |
108 | 113 | } |
109 | 114 |
|
110 | | -fn main() -> Result<()> { |
| 115 | +fn run() -> Result<()> { |
111 | 116 | let cli: Cli = Cli::parse(); |
112 | 117 |
|
113 | 118 | let ctx = Context::try_new().map_err(Error::NFC)?; |
114 | 119 | let device = ctx.open().map_err(Error::NFC)?; |
115 | | - let initiator = Initiator::try_from(device).map_err(Error::NFC)?; |
116 | | - let target = initiator.select_dep_target().map_err(Error::NFC)?; |
| 120 | + let initiator = Initiator::from(device); |
| 121 | + let target = initiator.select_dep_target(ctx).map_err(Error::NFC)?; |
117 | 122 |
|
118 | 123 | let nfc_card = NfcCard { target }; |
119 | 124 | let card = jpki::Card::new(Box::new(nfc_card)); |
120 | 125 | let jpki_ap = jpki::ap::JpkiAp::open((), Box::new(card)).map_err(Error::APDU)?; |
121 | 126 | match &cli.command { |
122 | 127 | SubCommand::ReadCertificate { ty } => { |
123 | | - let ty: jpki::ap::jpki::CertType = (*ty).into(); |
| 128 | + let ty: jpki_ap::CertType = (*ty).into(); |
124 | 129 | let pin = if ty.is_pin_required() { |
125 | 130 | prompt_password()? |
126 | 131 | } else { |
@@ -148,13 +153,24 @@ fn main() -> Result<()> { |
148 | 153 | let certificate = read_all(File::open(certificate_path).map_err(Error::IO)?)?; |
149 | 154 | let signature = read_all(File::open(signature_path).map_err(Error::IO)?)?; |
150 | 155 | if jpki::digest::verify(certificate, read_all(stdin())?, signature) { |
151 | | - println!("OK"); |
| 156 | + info!("OK") |
152 | 157 | } else { |
153 | | - println!("NG"); |
| 158 | + error!("NG"); |
154 | 159 | exit(1); |
155 | 160 | } |
156 | 161 | } |
157 | 162 | } |
158 | 163 |
|
159 | 164 | Ok(()) |
160 | 165 | } |
| 166 | + |
| 167 | +fn main() { |
| 168 | + tracing_subscriber::fmt::fmt() |
| 169 | + .with_env_filter(EnvFilter::from_default_env()) |
| 170 | + .with_writer(stderr) |
| 171 | + .init(); |
| 172 | + |
| 173 | + if let Err(e) = run() { |
| 174 | + error!("{}", e); |
| 175 | + } |
| 176 | +} |
0 commit comments