# rdnie
[](https://crates.io/crates/rdnie)
[](https://docs.rs/rdnie)
[](https://github.com/sgomez/rdnie/actions/workflows/ci.yml)
[](https://github.com/sgomez/rdnie/blob/main/LICENSE)
Rust driver for the Spanish electronic ID card (DNIe). It talks APDU to the card over PC/SC, without PKCS#11, OpenSC or system drivers, and behaves the same on Linux, macOS and Windows.
## What it does
- Finds a DNIe 3.0 or 4.0 among the readers by its ATR.
- Opens the CWA-14890 secure channel.
- Reads the certificates, and the holder's name, surnames and number from them.
- Reads the PIN retries left without sending the PIN.
- Signs a SHA-256, SHA-384 or SHA-512 digest with PKCS#1 v1.5.
DNIe 1.0 and 2.0, NFC and PACE, changing the PIN, decrypting and other cards are out of scope. [`docs/parity.md`](https://github.com/sgomez/rdnie/blob/main/docs/parity.md) compares it with JMulticard, the driver it is ported from.
## Performance
It reads only what is asked for and never resets the card, so it sends fewer APDUs than JMulticard and OpenSC. Measured with rdnie 0.3.0, JMulticard 2.1 and OpenSC 0.27 on one DNIe and reader, from connecting to holding the signature certificate:
| **rdnie** | **416 ms** | **42** |
| JMulticard | 1323 ms (3.2×) | 84 |
| OpenSC | 1526 ms (3.7×) | 130 |
Reading every certificate, as JMulticard does, rdnie takes 830 ms. Method and caveats: [`docs/benchmark.md`](https://github.com/sgomez/rdnie/blob/main/docs/benchmark.md).
## Compared with JMulticard
| Finding the DNIe | Resets each reader until one holds a DNIe | Reads each reader's ATR without a reset |
| Opening | Resets the card twice | No reset |
| OpenSC or Firefox on the same card | Lose their state | Keep working, checked on a real card |
| Reads on opening | MF, CDF, every certificate and PrKDF | The IDESP |
| One certificate | From the map loaded on opening | Reads only its file, once per session |
| Signing | Needs the certificate, for the modulus length | Reads no certificate |
| A corrupt certificate | Breaks the whole load | Fails only `certificate` for its label |
| The IDESP | Read again on every call | Read once, on opening |
| A lost channel while signing | Signs again | Returns the error; nothing retries |
## Usage
```sh
cargo add rdnie
```
```rust
use rdnie::{DigestAlgorithm, PcscTransport, Pin, SIGNATURE_LABEL, Session};
use sha2::{Digest, Sha256};
fn main() -> Result<(), rdnie::Error> {
let mut session = Session::open(PcscTransport::connect_first_dnie()?)?;
let signing = session
.private_key(SIGNATURE_LABEL)?
.expect("a DNIe has a signature key");
let retries = session.pin_retries_left()?;
let pin = Pin::new(ask_holder_for_pin(retries));
let digest = Sha256::digest(b"document");
let signature = session.sign(&digest, DigestAlgorithm::Sha256, &signing, pin)?;
Ok(())
}
```
`labels` and `private_key` read no certificate file: signing needs only the key. `session.certificate(label)` reads that one X.509 certificate on first request and keeps it for the session, and `Holder::from_der` reads the holder's data from it.
## The PIN
- **Never sent blind.** rdnie sends the PIN only if the caller read the retries with `pin_retries_left` in this session (`Error::PinRetriesNotRead` otherwise). `sign` reads them again before VERIFY and stops without sending the PIN if they are 0 (`Error::PinBlocked`) or differ from those the session expects (`Error::PinRetriesChanged`). Showing the holder the retries before asking for the PIN is the caller's job.
- **One attempt per call.** A wrong PIN returns `Error::WrongPin`, and nothing retries on its own, not even after a card reset.
- **Checked before the card.** A PIN outside 8 to 16 characters fails with `Error::InvalidPinFormat` without spending an attempt.
- **Wiped from memory.** `Pin` is zeroized when dropped, and no PIN or session key survives an operation.
## Platforms
| Linux | `pcscd` | `libpcsclite-dev` and `pkg-config` |
| macOS | nothing | nothing |
| Windows | nothing | nothing |
The minimum supported Rust version is 1.87.
## Features
`pcsc`, on by default, provides `PcscTransport`. With `default-features = false` the crate builds without PC/SC and runs over any implementation of the `Transport` trait.
## Security
Report vulnerabilities privately, as [`SECURITY.md`](https://github.com/sgomez/rdnie/blob/main/SECURITY.md) explains.
## Contributing
See [`CONTRIBUTING.md`](https://github.com/sgomez/rdnie/blob/main/CONTRIBUTING.md).
## License
Copyleft: free software under the [EUPL-1.2](https://github.com/sgomez/rdnie/blob/main/LICENSE). Parts are derived from [JMulticard](https://github.com/ctt-gob-es/jmulticard); see [`NOTICE`](https://github.com/sgomez/rdnie/blob/main/NOTICE).