rdnie 0.4.0

Spanish electronic ID card (DNIe) driver over PC/SC: CWA-14890 secure channel, certificates, PIN retry counter and signing
Documentation
// Derived from JMulticard: see LICENSE.jmulticard.md.
// The `AC RAIZ COMPONENTES 2` modulus comes from OpenSC
// (https://github.com/OpenSC/OpenSC, commit fc27ee8, src/libopensc/cwa-dnie.c,
// `icc_root_ca_modulus_1`, LGPL 2.1+).

//! Terminal key sets of the CWA-14890 channels, picked by key generation and channel, and the roots
//! of the chip's component certificates.

use rsa::{BigUint, RsaPrivateKey, RsaPublicKey, pkcs8::DecodePublicKey};

mod first_generation;
mod second_generation;

pub(crate) use first_generation::{R1_PIN, R1_USER};
pub(crate) use second_generation::{R2_PIN, R2_USER};

const PUBLIC_EXPONENT: u32 = 65_537;

/// Root CA public key the card checks the CA CVC with.
pub(crate) const CA_PUBLIC_KEY_REFERENCE: [u8; 2] = [0x02, 0x0F];
/// ICC private key of the internal authentication.
pub(crate) const ICC_PRIVATE_KEY_REFERENCE: [u8; 2] = [0x02, 0x1F];

/// `pk-RCAicc` (RSA-1024 SPKI), the `AC RAIZ COMPONENTES` root, from `Dnie3Cwa14890Constants`.
const COMPONENT_ROOT: [u8; 162] = [
    0x30, 0x81, 0x9F, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01,
    0x05, 0x00, 0x03, 0x81, 0x8D, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xEA, 0xDE, 0xDA,
    0x45, 0x53, 0x32, 0x94, 0x50, 0x39, 0xDA, 0xA4, 0x04, 0xC8, 0xEB, 0xC4, 0xD3, 0xB7, 0xF5, 0xDC,
    0x86, 0x92, 0x83, 0xCD, 0xEA, 0x2F, 0x10, 0x1E, 0x2A, 0xB5, 0x4F, 0xB0, 0xD0, 0xB0, 0x3D, 0x8F,
    0x03, 0x0D, 0xAF, 0x24, 0x58, 0x02, 0x82, 0x88, 0xF5, 0x4C, 0xE5, 0x52, 0xF8, 0xFA, 0x57, 0xAB,
    0x2F, 0xB1, 0x03, 0xB1, 0x12, 0x42, 0x7E, 0x11, 0x13, 0x1D, 0x1D, 0x27, 0xE1, 0x0A, 0x5B, 0x50,
    0x0E, 0xAA, 0xE5, 0xD9, 0x40, 0x30, 0x1E, 0x30, 0xEB, 0x26, 0xC3, 0xE9, 0x06, 0x6B, 0x25, 0x71,
    0x56, 0xED, 0x63, 0x9D, 0x70, 0xCC, 0xC0, 0x90, 0xB8, 0x63, 0xAF, 0xBB, 0x3B, 0xFE, 0xD8, 0xC1,
    0x7B, 0xE7, 0x67, 0x30, 0x34, 0xB9, 0x82, 0x3E, 0x97, 0x7E, 0xD6, 0x57, 0x25, 0x29, 0x27, 0xF9,
    0x57, 0x5B, 0x9F, 0xFF, 0x66, 0x91, 0xDB, 0x64, 0xF8, 0x0B, 0x5E, 0x92, 0xCD, 0x02, 0x03, 0x01,
    0x00, 0x01,
];

/// Modulus of the `AC RAIZ COMPONENTES 2` root, `icc_root_ca_modulus_1` of `OpenSC`.
const COMPONENT_ROOT_2_MODULUS: [u8; 128] = [
    0xB9, 0x72, 0x34, 0x5E, 0x35, 0xBC, 0xDD, 0x12, 0xDC, 0x2C, 0x8E, 0x85, 0xF6, 0x22, 0x97, 0x97,
    0x9F, 0x12, 0x2B, 0xB7, 0xC9, 0xC3, 0xED, 0x13, 0xA0, 0xC4, 0xEB, 0x59, 0x34, 0xE7, 0x0C, 0xD6,
    0xD0, 0x0C, 0x54, 0x06, 0x18, 0x38, 0x6E, 0x42, 0xF2, 0xBA, 0x00, 0x89, 0xC0, 0xDD, 0x80, 0x0E,
    0xBA, 0x78, 0x3B, 0xDC, 0x9D, 0x93, 0xD9, 0xFB, 0xFC, 0x3C, 0x16, 0x9F, 0x9A, 0xF6, 0x4E, 0x80,
    0x10, 0x0F, 0xC6, 0x87, 0xCC, 0xA5, 0x62, 0xE7, 0xFC, 0x84, 0xD1, 0x12, 0x92, 0xC2, 0x40, 0x4C,
    0x59, 0xB8, 0xA8, 0x60, 0xD3, 0x9E, 0x2D, 0x66, 0x54, 0x7D, 0xC7, 0xB2, 0xD4, 0x8C, 0xA7, 0x89,
    0x81, 0x4F, 0x43, 0x06, 0x26, 0x34, 0xE3, 0xE0, 0xC0, 0xD6, 0xBF, 0x5F, 0x54, 0xBA, 0x1D, 0x9C,
    0x46, 0x64, 0x45, 0x83, 0x1D, 0xCD, 0xEA, 0xB0, 0x87, 0x08, 0xF3, 0xF6, 0x22, 0x0E, 0x07, 0x75,
];

/// Roots the chip's component certificate chain may end in.
#[derive(Clone)]
pub(crate) struct ComponentRoots {
    first: RsaPublicKey,
    second: RsaPublicKey,
}

impl ComponentRoots {
    /// `AC RAIZ COMPONENTES` and `AC RAIZ COMPONENTES 2`, both also in the DGP's libpkcs11-dnie 1.6.8.
    pub(crate) fn embedded() -> Self {
        Self {
            first: RsaPublicKey::from_public_key_der(&COMPONENT_ROOT)
                .expect("the embedded component root is valid"),
            second: RsaPublicKey::new(
                BigUint::from_bytes_be(&COMPONENT_ROOT_2_MODULUS),
                BigUint::from(PUBLIC_EXPONENT),
            )
            .expect("the embedded second component root is valid"),
        }
    }

    #[cfg(any(test, feature = "emulator"))]
    pub(crate) fn only(root: RsaPublicKey) -> Self {
        Self {
            first: root.clone(),
            second: root,
        }
    }

    #[cfg(test)]
    pub(crate) fn pair(first: RsaPublicKey, second: RsaPublicKey) -> Self {
        Self { first, second }
    }

    /// The roots in the order to try them: `pk-RCAicc` first.
    pub(crate) fn iter(&self) -> impl Iterator<Item = &RsaPublicKey> {
        [&self.first, &self.second].into_iter()
    }
}

/// Key generation the DGP issued the terminal keys in.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum KeyGeneration {
    /// Cards with a support number below `BMP100001`.
    First,
    /// Later cards, and any card whose support number is unknown.
    Second,
}

/// Channel a key set opens.
#[derive(Clone, Copy)]
pub(crate) enum Channel {
    Pin,
    User,
}

/// Terminal side of one channel in one key generation.
pub(crate) struct KeySet {
    pub(crate) ca_cvc: &'static [u8],
    pub(crate) ca_chr: [u8; 8],
    pub(crate) terminal_cvc: &'static [u8],
    pub(crate) terminal_chr: [u8; 8],
    pub(crate) modulus: &'static [u8],
    private_exponent: &'static [u8],
}

impl KeySet {
    /// The set for `channel` in `generation`.
    pub(crate) fn for_generation(generation: KeyGeneration, channel: Channel) -> &'static Self {
        match (generation, channel) {
            (KeyGeneration::First, Channel::Pin) => &R1_PIN,
            (KeyGeneration::First, Channel::User) => &R1_USER,
            (KeyGeneration::Second, Channel::Pin) => &R2_PIN,
            (KeyGeneration::Second, Channel::User) => &R2_USER,
        }
    }

    /// The terminal private key, with its primes recovered from the modulus and exponents.
    pub(crate) fn private_key(&self) -> RsaPrivateKey {
        RsaPrivateKey::from_components(
            BigUint::from_bytes_be(self.modulus),
            BigUint::from(PUBLIC_EXPONENT),
            BigUint::from_bytes_be(self.private_exponent),
            vec![],
        )
        .expect("embedded terminal keys are valid RSA keys")
    }
}

#[cfg(test)]
mod tests;