openpgp_card_rpgp/
lib.rs

1// SPDX-FileCopyrightText: Wiktor Kwapisiewicz <wiktor@metacode.biz>
2// SPDX-FileCopyrightText: Heiko Schaefer <heiko@schaefer.name>
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! This is a crate for using
6//! [OpenPGP card devices](https://en.wikipedia.org/wiki/OpenPGP_card) with
7//! the [`rPGP`](https://crates.io/crates/pgp) OpenPGP library.
8//!
9//! In fact, this crate is a supplement for the
10//! [`openpgp-card`](https://crates.io/crates/openpgp-card) crate.
11//! This crate, `openpgp-card-rpgp`, enables performing OpenPGP-specific
12//! operations on cards, by leveraging both the `rPGP` library and `openpgp-card`.
13//! If you want to use this crate, you will probably also want to use
14//! `openpgp-card` itself:
15//!
16//! Much of the functionality of an OpenPGP card device doesn't actually
17//! involve the OpenPGP format. All of that functionality is available in
18//! `openpgp-card`, without requiring support for the OpenPGP format.
19//!
20//! This crate implements additional support for operations that *do* require
21//! handling the OpenPGP format:
22//!
23//! - Creating OpenPGP signatures
24//! - Decryption of OpenPGP data
25//! - Import of OpenPGP private key material
26//!
27//! See this project's "examples" for some pointers on how to use this crate.
28
29mod cardslot;
30mod private;
31mod rpgp;
32
33use std::fmt::Debug;
34
35pub use cardslot::CardSlot;
36pub use private::UploadableKey;
37pub use rpgp::{
38    bind_into_certificate, public_key_material_and_fp_to_key, public_key_material_to_key,
39    public_to_fingerprint,
40};
41
42/// Enum wrapper for the error types of this crate
43#[derive(thiserror::Error, Debug)]
44#[non_exhaustive]
45pub enum Error {
46    #[error("rPGP error: {0}")]
47    Rpgp(pgp::errors::Error),
48
49    #[error("OpenPGP card error: {0}")]
50    Ocard(openpgp_card::Error),
51
52    #[error("Internal error: {0}")]
53    Message(String),
54}
55
56impl From<Error> for pgp::errors::Error {
57    fn from(value: Error) -> Self {
58        pgp::errors::Error::Message {
59            message: format!("openpgp-card-rpgp error: {value:?}"),
60            backtrace: None,
61        }
62    }
63}
64
65impl From<pgp::errors::Error> for Error {
66    fn from(value: pgp::errors::Error) -> Self {
67        Error::Rpgp(value)
68    }
69}
70
71impl From<openpgp_card::Error> for Error {
72    fn from(value: openpgp_card::Error) -> Self {
73        Error::Ocard(value)
74    }
75}
76
77impl From<rsa::Error> for Error {
78    fn from(value: rsa::Error) -> Self {
79        Error::Message(format!("rsa error: {value:?}"))
80    }
81}
82
83impl From<p256::elliptic_curve::Error> for Error {
84    fn from(value: p256::elliptic_curve::Error) -> Self {
85        Error::Message(format!("elliptic_curve error: {value:?}"))
86    }
87}
88
89impl From<ed25519_dalek::ed25519::Error> for Error {
90    fn from(value: ed25519_dalek::ed25519::Error) -> Self {
91        Error::Message(format!("ed25519_dalek error: {value:?}"))
92    }
93}