1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use num_bigint_dig::BigUint;
use rand::rngs::OsRng;
use rsa::{PublicKeyParts, RSAPrivateKey, RSAPublicKey};
use std::fmt;

/// the purpose of this structure is to provide an implementation of BigUint, as is used by the rsa crate, that can be serialized for the sake of storing an retriving rsa keys
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BigNum {
    value: Vec<u8>,
}
impl BigNum {
    pub fn to_string_unstable(&self) -> String {
        unsafe { String::from_utf8_unchecked(self.value.clone()) }
    }
}
impl From<&BigUint> for BigNum {
    fn from(num: &BigUint) -> Self {
        Self {
            value: num.to_bytes_be(),
        }
    }
}
impl From<&BigNum> for BigUint {
    fn from(num: &BigNum) -> Self {
        BigUint::from_bytes_be(&num.value)
    }
}
impl PartialEq<BigUint> for BigNum {
    fn eq(&self, other: &BigUint) -> bool {
        self.value == other.to_bytes_be()
    }
}
impl PartialEq<BigNum> for BigUint {
    fn eq(&self, other: &BigNum) -> bool {
        self.to_bytes_be() == other.value
    }
}

impl fmt::Display for BigNum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.value)
    }
}
/// this struct is used within ArtificePeer, that is transmittted over then network so as to provide the public key of the peer to the hsot
/// like BigNum, this is an abstraction of RSAPublicKey that can be serialized using the serde crate
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct PubKeyComp {
    n: BigNum,
    e: BigNum,
}
impl PubKeyComp {
    pub fn from_parts(n: BigNum, e: BigNum) -> Self {
        Self { n, e }
    }
    pub fn n(&self) -> &BigNum {
        &self.n
    }
    pub fn e(&self) -> &BigNum {
        &self.e
    }
}
impl PartialEq<RSAPublicKey> for PubKeyComp {
    fn eq(&self, pubkey: &RSAPublicKey) -> bool {
        self.n == *pubkey.n() && self.e == *pubkey.e()
    }
}
impl PartialEq<PubKeyComp> for RSAPublicKey {
    fn eq(&self, pubkey: &PubKeyComp) -> bool {
        *self.n() == pubkey.n && *self.e() == pubkey.e
    }
}
impl From<&RSAPublicKey> for PubKeyComp {
    fn from(public_key: &RSAPublicKey) -> Self {
        Self::from_parts(BigNum::from(public_key.n()), BigNum::from(public_key.e()))
    }
}
impl From<&RSAPrivateKey> for PubKeyComp {
    fn from(private_key: &RSAPrivateKey) -> Self {
        Self::from(&RSAPublicKey::from(private_key))
    }
}
impl From<&PrivKeyComp> for PubKeyComp {
    fn from(priv_key: &PrivKeyComp) -> Self {
        Self {
            n: priv_key.n().to_owned(),
            e: priv_key.e().to_owned(),
        }
    }
}
/// private key version of PubKeyComp
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct PrivKeyComp {
    n: BigNum,
    e: BigNum,
    d: BigNum,
    primes: Vec<BigNum>,
}
impl PrivKeyComp {
    pub fn n(&self) -> &BigNum {
        &self.n
    }
    pub fn e(&self) -> &BigNum {
        &self.e
    }
    pub fn d(&self) -> &BigNum {
        &self.d
    }
    pub fn primes(&self) -> &Vec<BigNum> {
        &self.primes
    }
    pub fn into_components(self) -> (BigNum, BigNum, BigNum, Vec<BigNum>) {
        (self.n, self.e, self.d, self.primes)
    }
    /// used to create the key (should only be run once per host owing to the long execution time)
    /// designed for use in the installer only
    pub fn generate() -> rsa::errors::Result<Self> {
        let mut rng = OsRng;
        let bits = 2048;
        let private_key = RSAPrivateKey::new(&mut rng, bits)?;
        let d = BigNum::from(private_key.d());
        let primes: Vec<BigNum> = private_key
            .primes()
            .iter()
            .map(|p| BigNum::from(p))
            .collect();
        let n = BigNum::from(private_key.n());
        let e = BigNum::from(private_key.e());
        Ok(Self { n, e, d, primes })
    }
}
impl From<&RSAPrivateKey> for PrivKeyComp {
    fn from(key: &RSAPrivateKey) -> Self {
        let primes = key.primes().iter().map(|p| BigNum::from(p)).collect();
        let d = BigNum::from(key.d());
        let public_key = RSAPublicKey::from(key);
        let n = BigNum::from(public_key.n());
        let e = BigNum::from(public_key.e());
        Self { n, e, d, primes }
    }
}
impl From<&PrivKeyComp> for RSAPrivateKey {
    fn from(comp: &PrivKeyComp) -> RSAPrivateKey {
        RSAPrivateKey::from_components(
            comp.n().into(),
            comp.e().into(),
            comp.d().into(),
            comp.primes.iter().map(|p| BigUint::from(p)).collect(),
        )
    }
}