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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use super::{Key, PrivateParts, PublicParts};
use crate::error::{Error, ErrorKind, OsshResult};
use crate::format::ossh_pubkey::*;
use openssl::bn::BigNum;
use openssl::dsa::{Dsa, DsaRef};
use openssl::hash::MessageDigest;
use openssl::pkey::{PKey, Private, Public};
use openssl::sign::{Signer, Verifier};
use std::fmt;
pub const DSA_NAME: &str = "ssh-dss";
#[derive(Debug, Clone)]
pub struct DsaPublicKey {
dsa: Dsa<Public>,
}
impl DsaPublicKey {
pub fn new(
p: BigNum,
q: BigNum,
g: BigNum,
pub_key: BigNum,
) -> Result<Self, openssl::error::ErrorStack> {
let dsa = Dsa::from_public_components(p, q, g, pub_key)?;
Ok(Self { dsa })
}
pub(crate) fn from_ossl_dsa(key: Dsa<Public>) -> Self {
Self { dsa: key }
}
#[allow(unused)]
pub(crate) fn ossl_dsa(&self) -> &DsaRef<Public> {
&self.dsa
}
pub(crate) fn ossl_pkey(&self) -> Result<PKey<Public>, openssl::error::ErrorStack> {
PKey::from_dsa(self.dsa.clone())
}
}
impl Key for DsaPublicKey {
fn size(&self) -> usize {
self.dsa.p().num_bits() as usize
}
fn keyname(&self) -> &'static str {
DSA_NAME
}
}
impl PublicParts for DsaPublicKey {
fn blob(&self) -> Result<Vec<u8>, Error> {
encode_dsa_pubkey(&self.dsa)
}
fn verify(&self, data: &[u8], sig: &[u8]) -> Result<bool, Error> {
let pkey = PKey::from_dsa(self.dsa.clone())?;
let mut veri = Verifier::new(MessageDigest::sha1(), &pkey)?;
veri.update(data)?;
Ok(veri.verify(sig)?)
}
}
impl PartialEq for DsaPublicKey {
fn eq(&self, other: &Self) -> bool {
(self.dsa.p() == other.dsa.p())
&& (self.dsa.q() == other.dsa.q())
&& (self.dsa.g() == other.dsa.g())
&& (self.dsa.pub_key() == other.dsa.pub_key())
}
}
impl fmt::Display for DsaPublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&serialize_ossh_pubkey(self, "").unwrap())
}
}
pub struct DsaKeyPair {
dsa: Dsa<Private>,
}
impl DsaKeyPair {
pub(crate) fn from_ossl_dsa(key: Dsa<Private>) -> Self {
Self { dsa: key }
}
pub(crate) fn ossl_dsa(&self) -> &DsaRef<Private> {
&self.dsa
}
pub fn generate(mut bits: usize) -> OsshResult<Self> {
if bits == 0 {
bits = 1024;
}
if bits != 1024 {
return Err(Error::from_kind(ErrorKind::InvalidKeySize));
}
Ok(DsaKeyPair {
dsa: Dsa::generate(bits as u32)?,
})
}
pub fn clone_public_key(&self) -> Result<DsaPublicKey, Error> {
let p = self.dsa.p().to_owned()?;
let q = self.dsa.q().to_owned()?;
let g = self.dsa.g().to_owned()?;
let pub_key = self.dsa.pub_key().to_owned()?;
Ok(DsaPublicKey::new(p, q, g, pub_key)?)
}
}
impl Key for DsaKeyPair {
fn size(&self) -> usize {
self.dsa.p().num_bits() as usize
}
fn keyname(&self) -> &'static str {
DSA_NAME
}
}
impl PublicParts for DsaKeyPair {
fn blob(&self) -> Result<Vec<u8>, Error> {
encode_dsa_pubkey(&self.dsa)
}
fn verify(&self, data: &[u8], sig: &[u8]) -> Result<bool, Error> {
self.clone_public_key()?.verify(data, sig)
}
}
impl PrivateParts for DsaKeyPair {
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, Error> {
let pkey = PKey::from_dsa(self.dsa.clone())?;
let mut sign = Signer::new(MessageDigest::sha1(), &pkey)?;
sign.update(data)?;
Ok(sign.sign_to_vec()?)
}
}
#[allow(non_upper_case_globals)]
#[cfg(test)]
mod test {
use super::*;
use openssl::bn::BigNum;
const pub_str: &str = "ssh-dss AAAAB3NzaC1kc3MAAACBAORLYnYacOdGmSJ99aZ+j2UqtQldYNHvAVVAI42wt/T/GTkg8cXdwwQ8HSJyD6T1e9ebnCXZd/YItX8DCPIP5GLUHVZy5zzKSzwga7zEjKP2j3JZGLAzFIUpStwQ8gur3zmh5DYi7JOdc/kWNpjT86n4fnrP+s8ZxuVDO5bbSasHAAAAFQD62yfFzJxz313aoIVgoMFoz8cF/wAAAIEAj7rvQz2hmuRyFUZIGWpwVHoR3y3SoQjEryX4ZtzwL04ROIXHSKJeOY9cdu2l5fMVYiMBtfWTQTlltFl1H//0hG/g5KBLhhwQ3Y7ul4Q8wsCWZJZeP3jtcO7+p3BLyMa6vvv5ptnMH+jRMgX5wwdszqogk4jCT+7fM2p6brMGccoAAACAD9qfPNxRo+npg+troNZ/FoYJezECqxg0jUyHWClACt7gS0W+r3dJIn9te6Xi7UFGPrLWJtlC++8i27m2FTS0sQUljM2NmRaf6jrCAhwPaJ0ievPJm5kBQmprTqBbdzCNRpI1+hceAnoHbajRwLueFwpoVOy2QjTkvBzd84Oobtw=";
const p: [u8; 0x81] = [
0x00, 0xe4, 0x4b, 0x62, 0x76, 0x1a, 0x70, 0xe7, 0x46, 0x99, 0x22, 0x7d, 0xf5, 0xa6, 0x7e,
0x8f, 0x65, 0x2a, 0xb5, 0x09, 0x5d, 0x60, 0xd1, 0xef, 0x01, 0x55, 0x40, 0x23, 0x8d, 0xb0,
0xb7, 0xf4, 0xff, 0x19, 0x39, 0x20, 0xf1, 0xc5, 0xdd, 0xc3, 0x04, 0x3c, 0x1d, 0x22, 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, 0x83, 0xa8, 0x6e, 0xdc,
];
fn get_test_pubkey() -> Result<DsaPublicKey, Error> {
let dsa_p = BigNum::from_slice(&p)?;
let dsa_q = BigNum::from_slice(&q)?;
let dsa_g = BigNum::from_slice(&g)?;
let dsa_pub = BigNum::from_slice(&pub_key)?;
Ok(DsaPublicKey::new(dsa_p, dsa_q, dsa_g, dsa_pub)?)
}
#[test]
fn dsa_publickey_serialize() {
let key = get_test_pubkey().unwrap();
assert_eq!(key.to_string(), String::from(pub_str));
}
#[test]
fn dsa_publickey_size() {
let key = get_test_pubkey().unwrap();
assert_eq!(key.size(), 1024);
}
}