Simplify: use the changed CardBackend handling and the new openpgp-card-sequoia Card struct
This commit is contained in:
parent
15e7241807
commit
13b2b62f07
8 changed files with 41 additions and 45 deletions
|
@ -3,16 +3,15 @@
|
|||
|
||||
use anyhow::Result;
|
||||
|
||||
use openpgp_card::OpenPgp;
|
||||
use openpgp_card_pcsc::PcscBackend;
|
||||
use openpgp_card_sequoia::card::Open;
|
||||
use openpgp_card_sequoia::card::Card;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("The following OpenPGP cards are connected to your system:");
|
||||
|
||||
for card in PcscBackend::cards(None)? {
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let open = Open::new(pgp.transaction()?)?;
|
||||
for backend in PcscBackend::cards(None)? {
|
||||
let mut card = Card::new(backend);
|
||||
let open = card.transaction()?;
|
||||
println!(" {}", open.application_identifier()?.ident());
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@ documentation = "https://docs.rs/crate/openpgp-card-examples"
|
|||
[dependencies]
|
||||
sequoia-openpgp = "1.3"
|
||||
nettle = "7"
|
||||
openpgp-card = { path = "../openpgp-card" }
|
||||
openpgp-card-pcsc = { path = "../pcsc" }
|
||||
openpgp-card-sequoia = { path = "../openpgp-card-sequoia" }
|
||||
chrono = "0.4"
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2021 Wiktor Kwapisiewicz <wiktor@metacode.biz>
|
||||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use openpgp_card::OpenPgp;
|
||||
use openpgp_card_pcsc::PcscBackend;
|
||||
use openpgp_card_sequoia::card::Open;
|
||||
use openpgp_card_sequoia::card::Card;
|
||||
|
||||
use openpgp::parse::{stream::DecryptorBuilder, Parse};
|
||||
use openpgp::policy::StandardPolicy;
|
||||
|
@ -20,10 +19,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
let card_ident = &args[0];
|
||||
let pin_file = &args[1];
|
||||
|
||||
let card = PcscBackend::open_by_ident(card_ident, None)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let card_backend = PcscBackend::open_by_ident(card_ident, None)?;
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
let mut card = Card::new(card_backend);
|
||||
let mut open = card.transaction()?;
|
||||
|
||||
let pin = std::fs::read(pin_file)?;
|
||||
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2021 Wiktor Kwapisiewicz <wiktor@metacode.biz>
|
||||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use openpgp_card::OpenPgp;
|
||||
use openpgp_card_pcsc::PcscBackend;
|
||||
use openpgp_card_sequoia::card::Open;
|
||||
use openpgp_card_sequoia::card::Card;
|
||||
|
||||
use openpgp::serialize::stream::{Armorer, Message, Signer};
|
||||
use sequoia_openpgp as openpgp;
|
||||
|
@ -19,10 +18,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
let card_ident = &args[0];
|
||||
let pin_file = &args[1];
|
||||
|
||||
let card = PcscBackend::open_by_ident(card_ident, None)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let card_backend = PcscBackend::open_by_ident(card_ident, None)?;
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
let mut card = Card::new(card_backend);
|
||||
let mut open = card.transaction()?;
|
||||
|
||||
let pin = std::fs::read(pin_file)?;
|
||||
|
||||
|
|
|
@ -35,8 +35,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
let test_card_ident = env::var("TEST_CARD_IDENT");
|
||||
|
||||
if let Ok(test_card_ident) = test_card_ident {
|
||||
let card = PcscBackend::open_by_ident(&test_card_ident, None)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let backend = PcscBackend::open_by_ident(&test_card_ident, None)?;
|
||||
let mut pgp = OpenPgp::new(backend);
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
|
||||
|
@ -140,8 +140,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
// -----------------------------
|
||||
// Open fresh Card for decrypt
|
||||
// -----------------------------
|
||||
let card = PcscBackend::open_by_ident(&test_card_ident, None)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let backend = PcscBackend::open_by_ident(&test_card_ident, None)?;
|
||||
let mut pgp = OpenPgp::new(backend);
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
|
||||
|
@ -180,8 +180,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
// -----------------------------
|
||||
// Open fresh Card for signing
|
||||
// -----------------------------
|
||||
let card = PcscBackend::open_by_ident(&test_card_ident, None)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let backend = PcscBackend::open_by_ident(&test_card_ident, None)?;
|
||||
let mut pgp = OpenPgp::new(backend);
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
|
||||
|
@ -212,8 +212,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
|
||||
println!("The following OpenPGP cards are connected to your system:");
|
||||
|
||||
for card in PcscBackend::cards(None)? {
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
for backend in PcscBackend::cards(None)? {
|
||||
let mut pgp = OpenPgp::new(backend);
|
||||
|
||||
let open = Open::new(pgp.transaction()?)?;
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
//!
|
||||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! for card in PcscBackend::cards(None)? {
|
||||
//! let mut pgp = OpenPgp::new(Box::new(card));
|
||||
//! let mut pgp = OpenPgp::new(card);
|
||||
//! let mut open = Open::new(pgp.transaction()?)?;
|
||||
//! println!("Found OpenPGP card with ident '{}'",
|
||||
//! open.application_identifier()?.ident());
|
||||
|
@ -36,7 +36,7 @@
|
|||
//!
|
||||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! let card = PcscBackend::open_by_ident("abcd:12345678", None)?;
|
||||
//! let mut pgp = OpenPgp::new(Box::new(card));
|
||||
//! let mut pgp = OpenPgp::new(card);
|
||||
//! let mut open = Open::new(pgp.transaction()?)?;
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
|
@ -60,7 +60,7 @@
|
|||
//! // Open card via PCSC
|
||||
//! use sequoia_openpgp::policy::StandardPolicy;
|
||||
//! let card = PcscBackend::open_by_ident("abcd:12345678", None)?;
|
||||
//! let mut pgp = OpenPgp::new(Box::new(card));
|
||||
//! let mut pgp = OpenPgp::new(card);
|
||||
//! let mut open = Open::new(pgp.transaction()?)?;
|
||||
//!
|
||||
//! // Get authorization for user access to the card with password
|
||||
|
@ -98,7 +98,7 @@
|
|||
//! // Open card via PCSC
|
||||
//! use sequoia_openpgp::policy::StandardPolicy;
|
||||
//! let card = PcscBackend::open_by_ident("abcd:12345678", None)?;
|
||||
//! let mut pgp = OpenPgp::new(Box::new(card));
|
||||
//! let mut pgp = OpenPgp::new(card);
|
||||
//! let mut open = Open::new(pgp.transaction()?)?;
|
||||
//!
|
||||
//! // Get authorization for signing access to the card with password
|
||||
|
@ -125,7 +125,7 @@
|
|||
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
//! // Open card via PCSC
|
||||
//! let card = PcscBackend::open_by_ident("abcd:12345678", None)?;
|
||||
//! let mut pgp = OpenPgp::new(Box::new(card));
|
||||
//! let mut pgp = OpenPgp::new(card);
|
||||
//! let mut open = Open::new(pgp.transaction()?)?;
|
||||
//!
|
||||
//! // Get authorization for admin access to the card with password
|
||||
|
|
|
@ -18,7 +18,7 @@ use sequoia_openpgp::Cert;
|
|||
use openpgp_card::algorithm::AlgoSimple;
|
||||
use openpgp_card::card_do::{Sex, TouchPolicy};
|
||||
use openpgp_card::{CardBackend, KeyType, OpenPgp};
|
||||
use openpgp_card_sequoia::card::{Admin, Open};
|
||||
use openpgp_card_sequoia::card::{Admin, Card, Open};
|
||||
use openpgp_card_sequoia::util::{
|
||||
make_cert, public_key_material_and_fp_to_key, public_key_material_to_key,
|
||||
};
|
||||
|
@ -105,7 +105,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
user_pin,
|
||||
} => {
|
||||
let card = util::open_card(&ident)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
let user_pin = util::get_pin(&mut open, user_pin, ENTER_USER_PIN);
|
||||
|
@ -175,7 +175,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
cmd,
|
||||
} => {
|
||||
let card = util::open_card(&ident)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
let admin_pin = util::get_pin(&mut open, admin_pin, ENTER_ADMIN_PIN);
|
||||
|
@ -348,7 +348,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
}
|
||||
cli::Command::Pin { ident, cmd } => {
|
||||
let card = util::open_card(&ident)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
let pgpt = pgp.transaction()?;
|
||||
|
||||
let pinpad_modify = pgpt.feature_pinpad_modify();
|
||||
|
@ -562,7 +562,7 @@ fn list_cards() -> Result<()> {
|
|||
let cards = util::cards()?;
|
||||
if !cards.is_empty() {
|
||||
for card in cards {
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
let open = Open::new(pgp.transaction()?)?;
|
||||
println!(" {}", open.application_identifier()?.ident());
|
||||
}
|
||||
|
@ -574,7 +574,7 @@ fn list_cards() -> Result<()> {
|
|||
|
||||
fn set_identity(ident: &str, id: u8) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let card = util::open_card(ident)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
|
||||
let mut pgpt = pgp.transaction()?;
|
||||
pgpt.set_identity(id)?;
|
||||
|
@ -586,11 +586,11 @@ fn set_identity(ident: &str, id: u8) -> Result<(), Box<dyn std::error::Error>> {
|
|||
/// is plugged in, that card is returned. (We don't This
|
||||
fn pick_card_for_reading(ident: Option<String>) -> Result<Box<dyn CardBackend + Send + Sync>> {
|
||||
if let Some(ident) = ident {
|
||||
Ok(Box::new(util::open_card(&ident)?))
|
||||
Ok(util::open_card(&ident)?)
|
||||
} else {
|
||||
let mut cards = util::cards()?;
|
||||
if cards.len() == 1 {
|
||||
Ok(Box::new(cards.pop().unwrap()))
|
||||
Ok(cards.pop().unwrap())
|
||||
} else if cards.is_empty() {
|
||||
Err(anyhow::anyhow!("No cards found"))
|
||||
} else {
|
||||
|
@ -986,7 +986,7 @@ fn decrypt(
|
|||
let input = util::open_or_stdin(input)?;
|
||||
|
||||
let card = util::open_card(ident)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
|
||||
|
@ -1011,7 +1011,7 @@ fn sign_detached(
|
|||
let mut input = util::open_or_stdin(input)?;
|
||||
|
||||
let card = util::open_card(ident)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
|
||||
|
@ -1032,9 +1032,9 @@ fn sign_detached(
|
|||
fn factory_reset(ident: &str) -> Result<()> {
|
||||
println!("Resetting Card {}", ident);
|
||||
let card = util::open_card(ident)?;
|
||||
let mut pgp = OpenPgp::new(Box::new(card));
|
||||
let mut card = Card::new(card);
|
||||
|
||||
let mut open = Open::new(pgp.transaction()?)?;
|
||||
let mut open = card.transaction()?;
|
||||
open.factory_reset().map_err(|e| anyhow!(e))
|
||||
}
|
||||
|
||||
|
|
|
@ -10,12 +10,12 @@ use openpgp_card::{CardBackend, Error, StatusBytes};
|
|||
use openpgp_card_pcsc::PcscBackend;
|
||||
use openpgp_card_sequoia::card::{Admin, Open, Sign, User};
|
||||
|
||||
pub(crate) fn cards() -> Result<Vec<impl CardBackend>, Error> {
|
||||
PcscBackend::cards(None).map(|cards| cards.into_iter().collect())
|
||||
pub(crate) fn cards() -> Result<Vec<Box<dyn CardBackend + Send + Sync>>, Error> {
|
||||
PcscBackend::cards(None).map(|cards| cards.into_iter().map(|c| c.into()).collect())
|
||||
}
|
||||
|
||||
pub(crate) fn open_card(ident: &str) -> Result<impl CardBackend, Error> {
|
||||
PcscBackend::open_by_ident(ident, None)
|
||||
pub(crate) fn open_card(ident: &str) -> Result<Box<dyn CardBackend + Send + Sync>, Error> {
|
||||
Ok(PcscBackend::open_by_ident(ident, None)?.into())
|
||||
}
|
||||
|
||||
/// Get pin from file. Or via user input, if no file and no pinpad is available.
|
||||
|
|
Loading…
Reference in a new issue