Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2021-2023 Heiko Schaefer <[email protected]>
2 // SPDX-License-Identifier: MIT OR Apache-2.0
3
4 //! A thin abstraction layer for accessing smart cards, including, but not
5 //! limited to, [openpgp-card](https://gitlab.com/openpgp-card/openpgp-card)
6 //! devices.
7
8 /// This trait defines a connection with a smart card via a
9 /// backend implementation (e.g. via the pcsc backend in the crate
10 /// [card-backend-pcsc](https://crates.io/crates/card-backend-pcsc)).
11 ///
12 /// A [CardBackend] is only used to get access to a [CardTransaction] object,
13 /// which supports transmitting commands to the card.
14 pub trait CardBackendrust-analyzer cargo card-backend 0.2.0 CardBackend# {
15 /// If a CardBackend introduces a additional (possibly backend-specific)
16 /// limits for any fields in CardCaps, this fn can indicate that limit by
17 /// returning an amended [`CardCaps`].
18 fn limit_card_capsrust-analyzer cargo card-backend 0.2.0 CardBackend#limit_card_caps().(&self, card_caps: CardCapsrust-analyzer cargo card-backend 0.2.0 CardCaps#) -> CardCapsrust-analyzer cargo card-backend 0.2.0 CardCaps#;
19
20 fn transactionrust-analyzer cargo card-backend 0.2.0 CardBackend#transaction().(
21 &mut self,
22 reselect_application: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&[u8]>,
23 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn CardTransactionrust-analyzer cargo card-backend 0.2.0 CardTransaction# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + '_>, SmartcardErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#>;
24 }
25
26 /// The CardTransaction trait defines communication with a smart card via a
27 /// backend implementation (e.g. the pcsc backend in the crate
28 /// [card-backend-pcsc](https://crates.io/crates/card-backend-pcsc)),
29 /// after opening a transaction from a CardBackend.
30 pub trait CardTransactionrust-analyzer cargo card-backend 0.2.0 CardTransaction# {
31 /// Transmit the command data in `cmd` to the card.
32 ///
33 /// `buf_size` is a hint to the backend (the backend may ignore it)
34 /// indicating the expected maximum response size.
35 fn transmitrust-analyzer cargo card-backend 0.2.0 CardTransaction#transmit().(&mut self, cmd: &[u8], buf_size: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, SmartcardErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#>;
36
37 /// Select `application` on the card
38 fn selectrust-analyzer cargo card-backend 0.2.0 CardTransaction#select().(&mut self, application: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, SmartcardErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#> {
39 let mut cmd = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0x00, 0xa4, 0x04, 0x00]; // CLA, INS, P1, P2
40 cmd.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(application.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() as u8); // Lc
41 cmd.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(application); // Data
42 cmd.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0x00); // Le
43
44 self.transmitrust-analyzer cargo card-backend 0.2.0 CardTransaction#transmit().(&cmd, 254)
45 }
46
47 /// If a CardTransaction implementation introduces an additional,
48 /// backend-specific limit for maximum number of bytes per command,
49 /// this fn can indicate that limit by returning `Some(max_cmd_len)`.
50 fn max_cmd_lenrust-analyzer cargo card-backend 0.2.0 CardTransaction#max_cmd_len().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> {
51 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
52 }
53
54 /// Does the reader support FEATURE_VERIFY_PIN_DIRECT?
55 fn feature_pinpad_verifyrust-analyzer cargo card-backend 0.2.0 CardTransaction#feature_pinpad_verify().(&self) -> bool;
56
57 /// Does the reader support FEATURE_MODIFY_PIN_DIRECT?
58 fn feature_pinpad_modifyrust-analyzer cargo card-backend 0.2.0 CardTransaction#feature_pinpad_modify().(&self) -> bool;
59
60 /// Verify the PIN `pin` via the reader pinpad
61 fn pinpad_verifyrust-analyzer cargo card-backend 0.2.0 CardTransaction#pinpad_verify().(
62 &mut self,
63 pin: PinTyperust-analyzer cargo card-backend 0.2.0 PinType#,
64 card_caps: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CardCapsrust-analyzer cargo card-backend 0.2.0 CardCaps#>,
65 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, SmartcardErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#>;
66
67 /// Modify the PIN `pin` via the reader pinpad
68 fn pinpad_modifyrust-analyzer cargo card-backend 0.2.0 CardTransaction#pinpad_modify().(
69 &mut self,
70 pin: PinTyperust-analyzer cargo card-backend 0.2.0 PinType#,
71 card_caps: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CardCapsrust-analyzer cargo card-backend 0.2.0 CardCaps#>,
72 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, SmartcardErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#>;
73
74 /// Has a reset been detected while starting this transaction?
75 ///
76 /// (Backends may choose to always return false)
77 fn was_resetrust-analyzer cargo card-backend 0.2.0 CardTransaction#was_reset().(&self) -> bool;
78 }
79
80 /// Information about the capabilities of a card.
81 ///
82 /// CardCaps is used to signal capabilities (chaining, extended length support, max
83 /// command/response sizes, max PIN lengths) of the current card to backends.
84 ///
85 /// CardCaps is not intended for users of this library.
86 ///
87 /// (The information is gathered from the "Card Capabilities", "Extended length information" and
88 /// "PWStatus" DOs)
89 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
90 pub struct CardCapsrust-analyzer cargo card-backend 0.2.0 CardCaps# {
91 ext_supportrust-analyzer cargo card-backend 0.2.0 CardCaps#ext_support.: bool,
92 chaining_supportrust-analyzer cargo card-backend 0.2.0 CardCaps#chaining_support.: bool,
93 max_cmd_bytesrust-analyzer cargo card-backend 0.2.0 CardCaps#max_cmd_bytes.: u16,
94 max_rsp_bytesrust-analyzer cargo card-backend 0.2.0 CardCaps#max_rsp_bytes.: u16,
95 pw1_max_lenrust-analyzer cargo card-backend 0.2.0 CardCaps#pw1_max_len.: u8,
96 pw3_max_lenrust-analyzer cargo card-backend 0.2.0 CardCaps#pw3_max_len.: u8,
97 }
98
99 impl CardCapsrust-analyzer cargo card-backend 0.2.0 CardCaps# {
100 pub fn newrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps]new().(
101 ext_support: bool,
102 chaining_support: bool,
103 max_cmd_bytes: u16,
104 max_rsp_bytes: u16,
105 pw1_max_len: u8,
106 pw3_max_len: u8,
107 ) -> Selfrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps] {
108 Selfrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps] {
109 ext_supportrust-analyzer cargo card-backend 0.2.0 CardCaps#ext_support.,
110 chaining_supportrust-analyzer cargo card-backend 0.2.0 CardCaps#chaining_support.,
111 max_cmd_bytesrust-analyzer cargo card-backend 0.2.0 CardCaps#max_cmd_bytes.,
112 max_rsp_bytesrust-analyzer cargo card-backend 0.2.0 CardCaps#max_rsp_bytes.,
113 pw1_max_lenrust-analyzer cargo card-backend 0.2.0 CardCaps#pw1_max_len.,
114 pw3_max_lenrust-analyzer cargo card-backend 0.2.0 CardCaps#pw3_max_len.,
115 }
116 }
117
118 /// Does the card support extended Lc and Le fields?
119 pub fn ext_supportrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps]ext_support().(&self) -> bool {
120 self.ext_supportrust-analyzer cargo card-backend 0.2.0 CardCaps#ext_support.
121 }
122
123 /// Does the card support command chaining?
124 pub fn chaining_supportrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps]chaining_support().(&self) -> bool {
125 self.chaining_supportrust-analyzer cargo card-backend 0.2.0 CardCaps#chaining_support.
126 }
127
128 /// Maximum number of bytes in a command APDU
129 pub fn max_cmd_bytesrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps]max_cmd_bytes().(&self) -> u16 {
130 self.max_cmd_bytesrust-analyzer cargo card-backend 0.2.0 CardCaps#max_cmd_bytes.
131 }
132
133 /// Maximum number of bytes in a response APDU
134 pub fn max_rsp_bytesrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps]max_rsp_bytes().(&self) -> u16 {
135 self.max_rsp_bytesrust-analyzer cargo card-backend 0.2.0 CardCaps#max_rsp_bytes.
136 }
137
138 /// Maximum length of PW1
139 pub fn pw1_max_lenrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps]pw1_max_len().(&self) -> u8 {
140 self.pw1_max_lenrust-analyzer cargo card-backend 0.2.0 CardCaps#pw1_max_len.
141 }
142
143 /// Maximum length of PW3
144 pub fn pw3_max_lenrust-analyzer cargo card-backend 0.2.0 impl#[CardCaps]pw3_max_len().(&self) -> u8 {
145 self.pw3_max_lenrust-analyzer cargo card-backend 0.2.0 CardCaps#pw3_max_len.
146 }
147 }
148
149 /// Specify a PIN to *verify* (distinguishes between `Sign`, `User` and `Admin`).
150 ///
151 /// (Note that for PIN *management*, in particular changing a PIN, "signing and user" are
152 /// not distinguished. They always share the same PIN value `PW1`)
153 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
154 pub enum PinTyperust-analyzer cargo card-backend 0.2.0 PinType# {
155 /// Verify PW1 in mode P2=81 (for the PSO:CDS operation)
156 Signrust-analyzer cargo card-backend 0.2.0 PinType#Sign#,
157
158 /// Verify PW1 in mode P2=82 (for all other User operations)
159 Userrust-analyzer cargo card-backend 0.2.0 PinType#User#,
160
161 /// Verify PW3 (for Admin operations)
162 Adminrust-analyzer cargo card-backend 0.2.0 PinType#Admin#,
163 }
164
165 impl PinTyperust-analyzer cargo card-backend 0.2.0 PinType# {
166 pub fn idrust-analyzer cargo card-backend 0.2.0 impl#[PinType]id().(&self) -> u8 {
167 match self {
168 PinTyperust-analyzer cargo card-backend 0.2.0 PinType#::Signrust-analyzer cargo card-backend 0.2.0 PinType#Sign# => 0x81,
169 PinTyperust-analyzer cargo card-backend 0.2.0 PinType#::Userrust-analyzer cargo card-backend 0.2.0 PinType#User# => 0x82,
170 PinTyperust-analyzer cargo card-backend 0.2.0 PinType#::Adminrust-analyzer cargo card-backend 0.2.0 PinType#Admin# => 0x83,
171 }
172 }
173 }
174
175 /// Errors on the smartcard/reader layer
176 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(thiserrorrust-analyzer cargo thiserror 1.0.69 crate/::Errorrust-analyzer cargo thiserror-impl 1.0.69 Error:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
177 #[non_exhaustive]
178 pub enum SmartcardErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError# {
179 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("Failed to create a pcsc smartcard context {0}")]
180 ContextErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#ContextError#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
181
182 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("Failed to list readers: {0}")]
183 ReaderErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#ReaderError#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
184
185 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("No reader found.")]
186 NoReaderFoundErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#NoReaderFoundError#,
187
188 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("The requested card '{0}' was not found.")]
189 CardNotFoundrust-analyzer cargo card-backend 0.2.0 SmartcardError#CardNotFound#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
190
191 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("Failed to connect to the card: {0}")]
192 SmartCardConnectionErrorrust-analyzer cargo card-backend 0.2.0 SmartcardError#SmartCardConnectionError#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
193
194 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("Smart card status: [{0}, {1}]")]
195 SmartCardStatusrust-analyzer cargo card-backend 0.2.0 SmartcardError#SmartCardStatus#(u8, u8),
196
197 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("NotTransacted (SCARD_E_NOT_TRANSACTED)")]
198 NotTransactedrust-analyzer cargo card-backend 0.2.0 SmartcardError#NotTransacted#,
199
200 #[errorrust-analyzer cargo thiserror-impl 1.0.69 error:("Generic SmartCard Error: {0}")]
201 Errorrust-analyzer cargo card-backend 0.2.0 SmartcardError#Error#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
202 }