Support removing the card while waiting for a PIN

This commit is contained in:
puck 2026-03-06 09:57:49 +00:00
parent 04127f60b0
commit 20fc4719f3
2 changed files with 143 additions and 113 deletions

View file

@ -2,7 +2,7 @@ use std::{collections::HashMap, env::args, thread, time::Duration};
use der::{Any, Decode, asn1::SetOfVec, oid::ObjectIdentifier}; use der::{Any, Decode, asn1::SetOfVec, oid::ObjectIdentifier};
use openssl::{bn::BigNumContext, ec::PointConversionForm, pkey::PKey}; use openssl::{bn::BigNumContext, ec::PointConversionForm, pkey::PKey};
use tokio::runtime::Runtime; use tokio::{runtime::Runtime, select};
use url::Url; use url::Url;
use crate::{ use crate::{
@ -121,8 +121,9 @@ async fn run_auth(
}) })
.await; .await;
ctg_pipe.send(pipe::CardToGUI::WaitForCard).await;
let mut finder = PCSCCardFinder::new(); let mut finder = PCSCCardFinder::new();
let (mut crad, creds) = 'outer: loop {
ctg_pipe.send(pipe::CardToGUI::WaitForCard).await;
let mut crad = loop { let mut crad = loop {
let mut crad = finder.find_valid().await; let mut crad = finder.find_valid().await;
@ -145,8 +146,8 @@ async fn run_auth(
if ef_dir.is_none() if ef_dir.is_none()
|| !ef_dir.unwrap().windows(14).any(|w| { || !ef_dir.unwrap().windows(14).any(|w| {
w == [ w == [
0xA0, 0x00, 0x00, 0x07, 0x88, 0x50, 0x43, 0x41, 0x2D, 0x65, 0x4D, 0x52, 0x54, 0xA0, 0x00, 0x00, 0x07, 0x88, 0x50, 0x43, 0x41, 0x2D, 0x65, 0x4D, 0x52,
0x44, 0x54, 0x44,
] ]
}) })
{ {
@ -168,7 +169,8 @@ async fn run_auth(
&mut crad, &mut crad,
0, 0,
iso7816::SelectFile::DedicatedFileName(&[ iso7816::SelectFile::DedicatedFileName(&[
0xA0, 0x00, 0x00, 0x07, 0x88, 0x50, 0x43, 0x41, 0x2D, 0x65, 0x4D, 0x52, 0x54, 0x44, 0xA0, 0x00, 0x00, 0x07, 0x88, 0x50, 0x43, 0x41, 0x2D, 0x65, 0x4D, 0x52, 0x54,
0x44,
]), ]),
iso7816::SelectOccurrence::First, iso7816::SelectOccurrence::First,
) )
@ -226,7 +228,16 @@ async fn run_auth(
.await; .await;
} }
let GUIToCard::PIN(pin) = gtc_pipe.recv().await.unwrap(); let wait_until_not_present = crad.wait_until_not_present();
let next_command = gtc_pipe.recv();
select! {
_ = wait_until_not_present => {
continue 'outer;
}
val = next_command => {
let GUIToCard::PIN(pin) = val.unwrap();
ctg_pipe ctg_pipe
.send(pipe::CardToGUI::ProcessingMessage { .send(pipe::CardToGUI::ProcessingMessage {
message: String::from("Negotiating with the card..."), message: String::from("Negotiating with the card..."),
@ -243,6 +254,11 @@ async fn run_auth(
Err(pace::PACEStatus::CardError(n)) => return Err(n), Err(pace::PACEStatus::CardError(n)) => return Err(n),
_ => (), _ => (),
} }
}
}
};
break (crad, creds);
}; };
let apdus; let apdus;

View file

@ -1,8 +1,10 @@
use std::{collections::HashMap, ffi::CString};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::{collections::HashMap, ffi::CString};
use pcsc::{PNP_NOTIFICATION, Protocols, ReaderState, State}; use pcsc::{PNP_NOTIFICATION, Protocols, ReaderState, State, Status};
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
use tokio::time::sleep;
use crate::{Card, ResultAPDU}; use crate::{Card, ResultAPDU};
@ -81,6 +83,18 @@ impl PCSCCard {
} }
} }
} }
pub async fn wait_until_not_present(&self) {
loop {
sleep(Duration::from_millis(500)).await;
let Ok(status) = self.card.status2_owned() else {
break;
};
if !status.status().contains(Status::PRESENT) {
break;
}
}
}
} }
struct PCSCCardFinderInner { struct PCSCCardFinderInner {