rsp6_decoder/
keys.rs

1//! Decoding and using the RSA public keys needed to decrypt the ticket data.
2
3use num_bigint::BigUint;
4use serde::{Deserialize, Deserializer};
5use std::collections::HashMap;
6
7static KEYS_JSON: &'static str = include_str!("../keys.json");
8
9fn deserialize_hex_as_biguint<'de, D>(deserializer: D) -> Result<BigUint, D::Error>
10where
11    D: Deserializer<'de>,
12{
13    let buf = String::deserialize(deserializer)?;
14
15    BigUint::parse_bytes(buf.as_bytes(), 16).ok_or(serde::de::Error::custom(format!(
16        "failed to parse exponent hex"
17    )))
18}
19
20#[derive(Deserialize, Debug)]
21pub struct IssuerKey {
22    pub is_private: bool,
23    pub is_test: bool,
24    pub issuer_id: String,
25    #[serde(
26        rename = "modulus_hex",
27        deserialize_with = "deserialize_hex_as_biguint"
28    )]
29    pub modulus: BigUint,
30    #[serde(
31        rename = "public_exponent_hex",
32        deserialize_with = "deserialize_hex_as_biguint"
33    )]
34    pub public_exponent: BigUint,
35    pub public_key_x509: Option<String>,
36}
37
38#[derive(Debug)]
39pub struct IssuerKeyStore {
40    pub keys: HashMap<String, Vec<IssuerKey>>,
41}
42
43impl IssuerKeyStore {
44    pub fn new() -> Self {
45        let keys = serde_json::from_str(KEYS_JSON).expect("failed to parse embedded keys.json");
46        Self { keys }
47    }
48
49    /*
50    pub fn get_key(&self, issuer: &str) -> Vec<RsaPublicKey> {
51        let issuer_key = match self.keys.get(issuer) {
52            Some(x) => x,
53            None => return vec![],
54        };
55        let modulus = BigUint::from_bytes_be(&issuer_key.modulus);
56        let exponent = BigUint::from(issuer_key.public_exponent);
57        RsaPublicKey::new(modulus, exponent).expect("failed to parse embedded RSA public key")
58    }
59
60     */
61}