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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use super::error::ProtoError;
use super::key_type::{KeyType, KeyTypeEnum};
use super::private_key::*;
use serde::de::{Deserializer, Error};
use serde::ser::{SerializeTuple, Serializer};
use serde::{Deserialize, Serialize};

pub type MpInt = Vec<u8>;

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct RsaPublicKey {
    pub e: MpInt,
    pub n: MpInt,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct DssPublicKey {
    pub p: MpInt,
    pub q: MpInt,
    pub g: MpInt,
    pub y: MpInt,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct EcDsaPublicKey {
    pub identifier: String,
    pub q: MpInt,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct SkEcDsaPublicKey {
    pub identifier: String,
    pub q: MpInt,
    pub application: String,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Ed25519PublicKey {
    pub enc_a: Vec<u8>,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct SkEd25519PublicKey {
    pub enc_a: Vec<u8>,
    pub application: String,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum PublicKey {
    Dss(DssPublicKey),
    Ed25519(Ed25519PublicKey),
    SkEd25519(SkEd25519PublicKey),
    Rsa(RsaPublicKey),
    EcDsa(EcDsaPublicKey),
    SkEcDsa(SkEcDsaPublicKey),
}

impl KeyType for RsaPublicKey {
    const KEY_TYPE: &'static str = RsaPrivateKey::KEY_TYPE;
}

impl KeyType for DssPublicKey {
    const KEY_TYPE: &'static str = DssPrivateKey::KEY_TYPE;
}

impl KeyType for Ed25519PublicKey {
    const KEY_TYPE: &'static str = Ed25519PrivateKey::KEY_TYPE;
}

impl KeyType for EcDsaPublicKey {
    const KEY_TYPE: &'static str = EcDsaPrivateKey::KEY_TYPE;

    fn key_type(&self) -> String {
        format!("{}-{}", Self::KEY_TYPE, self.identifier)
    }
}

impl KeyType for SkEd25519PublicKey {
    const KEY_TYPE: &'static str = "sk-ssh-ed25519@openssh.com";
}

impl KeyType for SkEcDsaPublicKey {
    const KEY_TYPE: &'static str = "sk-ecdsa-sha2";

    fn key_type(&self) -> String {
        format!("{}-{}@openssh.com", Self::KEY_TYPE, self.identifier)
    }
}

impl From<PrivateKey> for PublicKey {
    fn from(key: PrivateKey) -> Self {
        match key {
            PrivateKey::Dss(key) => PublicKey::Dss(DssPublicKey::from(key)),
            PrivateKey::Ed25519(key) => PublicKey::Ed25519(Ed25519PublicKey::from(key)),
            PrivateKey::SkEd25519(key) => PublicKey::SkEd25519(SkEd25519PublicKey::from(key)),
            PrivateKey::Rsa(key) => PublicKey::Rsa(RsaPublicKey::from(key)),
            PrivateKey::EcDsa(key) => PublicKey::EcDsa(EcDsaPublicKey::from(key)),
            PrivateKey::SkEcDsa(key) => PublicKey::SkEcDsa(SkEcDsaPublicKey::from(key)),
        }
    }
}

impl From<RsaPrivateKey> for RsaPublicKey {
    fn from(key: RsaPrivateKey) -> Self {
        Self { e: key.e, n: key.n }
    }
}

impl From<DssPrivateKey> for DssPublicKey {
    fn from(key: DssPrivateKey) -> Self {
        Self {
            p: key.p,
            q: key.q,
            g: key.g,
            y: key.y,
        }
    }
}

impl From<EcDsaPrivateKey> for EcDsaPublicKey {
    fn from(key: EcDsaPrivateKey) -> Self {
        Self {
            identifier: key.identifier,
            q: key.q,
        }
    }
}

impl From<SkEcDsaPrivateKey> for SkEcDsaPublicKey {
    fn from(key: SkEcDsaPrivateKey) -> Self {
        Self {
            identifier: key.identifier,
            q: key.q,
            application: key.application,
        }
    }
}

impl From<Ed25519PrivateKey> for Ed25519PublicKey {
    fn from(key: Ed25519PrivateKey) -> Self {
        Self { enc_a: key.enc_a }
    }
}

impl From<SkEd25519PrivateKey> for SkEd25519PublicKey {
    fn from(key: SkEd25519PrivateKey) -> Self {
        Self {
            enc_a: key.enc_a,
            application: key.application,
        }
    }
}

impl From<&PrivateKey> for PublicKey {
    fn from(key: &PrivateKey) -> Self {
        Self::from(key.clone())
    }
}

impl From<&RsaPrivateKey> for RsaPublicKey {
    fn from(key: &RsaPrivateKey) -> Self {
        Self::from(key.clone())
    }
}

impl From<&DssPrivateKey> for DssPublicKey {
    fn from(key: &DssPrivateKey) -> Self {
        Self::from(key.clone())
    }
}

impl From<&EcDsaPrivateKey> for EcDsaPublicKey {
    fn from(key: &EcDsaPrivateKey) -> Self {
        Self::from(key.clone())
    }
}

impl From<&Ed25519PrivateKey> for Ed25519PublicKey {
    fn from(key: &Ed25519PrivateKey) -> Self {
        Self::from(key.clone())
    }
}

impl_key_type_enum_ser_de!(
    PublicKey,
    (PublicKey::Dss, DssPublicKey),
    (PublicKey::Rsa, RsaPublicKey),
    (PublicKey::EcDsa, EcDsaPublicKey),
    (PublicKey::SkEcDsa, SkEcDsaPublicKey),
    (PublicKey::Ed25519, Ed25519PublicKey),
    (PublicKey::SkEd25519, SkEd25519PublicKey)
);