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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use openssl::pkey::PKey;
use openssl::rsa::Rsa;
use openssl::sign::{Signer, Verifier};
use openssl::pkey::Private;
use openssl::x509::X509;
use std::fmt;
use std::fs::File;
use std::io::prelude::*;
#[derive(Copy, Clone, Debug)]
pub enum SigningAlgorithm {
Sha1,
Sha224,
Sha256,
Sha384,
Sha512,
InvalidAlgorithm,
}
impl From<SigningAlgorithm> for openssl::hash::MessageDigest {
fn from(src: SigningAlgorithm) -> openssl::hash::MessageDigest {
match src {
SigningAlgorithm::Sha1 => openssl::hash::MessageDigest::sha1(),
SigningAlgorithm::Sha224 => openssl::hash::MessageDigest::sha224(),
SigningAlgorithm::Sha256 => openssl::hash::MessageDigest::sha256(),
SigningAlgorithm::Sha384 => openssl::hash::MessageDigest::sha384(),
SigningAlgorithm::Sha512 => openssl::hash::MessageDigest::sha512(),
SigningAlgorithm::InvalidAlgorithm => panic!("How did you even get here?"),
}
}
}
impl fmt::Display for SigningAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl From<String> for SigningAlgorithm {
fn from(s: String) -> Self {
match s.to_lowercase().as_str() {
"http://www.w3.org/2000/09/xmldsig#rsa-sha1" => Self::Sha1,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha224" => Self::Sha224,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" => Self::Sha256,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha384" => Self::Sha384,
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512" => Self::Sha512,
_ => Self::InvalidAlgorithm,
}
}
}
impl From<SigningAlgorithm> for String {
fn from(sa: SigningAlgorithm) -> String {
match sa {
SigningAlgorithm::Sha1 => String::from("http://www.w3.org/2000/09/xmldsig#rsa-sha1"),
SigningAlgorithm::Sha224 => {
String::from("http://www.w3.org/2001/04/xmldsig-more#rsa-sha224")
}
SigningAlgorithm::Sha256 => {
String::from("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256")
}
SigningAlgorithm::Sha384 => {
String::from("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384")
}
SigningAlgorithm::Sha512 => {
String::from("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512")
}
_ => {
let result = format!("Invalid Algorithm specified: {:?}", sa);
log::error!("{}", result);
result
}
}
}
}
pub fn load_key_from_filename(key_filename: &str) -> Result<PKey<Private>, String> {
let mut f = match File::open(key_filename) {
Ok(value) => value,
Err(error) => return Err(format!("Error loading file {}: {:?}", &key_filename, error)),
};
let mut pkey_buffer = Vec::new();
match f.read_to_end(&mut pkey_buffer) {
Ok(_) => eprintln!("Read private key OK"),
Err(error) => {
return Err(format!("Failed to read private key? {:?}", error));
}
}
debug!("key: {}", key_filename);
let keypair = match Rsa::private_key_from_pem(&pkey_buffer) {
Ok(value) => value,
Err(error) => {
return Err(format!("Failed to load pkey from pem bytes: {:?}", error));
}
};
match PKey::from_rsa(keypair) {
Ok(value) => Ok(value),
Err(error) => Err(format!("Failed to convert into PKey object: {:?}", error)),
}
}
#[derive(Copy, Clone, Debug)]
pub enum DigestAlgorithm {
Sha1,
Sha224,
Sha256,
Sha384,
Sha512,
InvalidAlgorithm,
}
impl fmt::Display for DigestAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl From<DigestAlgorithm> for openssl::hash::MessageDigest {
fn from(src: DigestAlgorithm) -> openssl::hash::MessageDigest {
match src {
DigestAlgorithm::Sha1 => openssl::hash::MessageDigest::sha1(),
DigestAlgorithm::Sha224 => openssl::hash::MessageDigest::sha224(),
DigestAlgorithm::Sha256 => openssl::hash::MessageDigest::sha256(),
DigestAlgorithm::Sha384 => openssl::hash::MessageDigest::sha384(),
DigestAlgorithm::Sha512 => openssl::hash::MessageDigest::sha512(),
DigestAlgorithm::InvalidAlgorithm => panic!("How did you even get here?"),
}
}
}
impl From<openssl::hash::MessageDigest> for DigestAlgorithm {
fn from(_md: openssl::hash::MessageDigest) -> Self {
Self::InvalidAlgorithm
}
}
impl From<String> for DigestAlgorithm {
fn from(s: String) -> Self {
match s.to_lowercase().as_str() {
"http://www.w3.org/2000/09/xmldsig#sha1" => Self::Sha1,
"http://www.w3.org/2001/04/xmlenc#sha256" => Self::Sha256,
"http://www.w3.org/2001/04/xmldsig-more#sha224" => Self::Sha224,
"http://www.w3.org/2001/04/xmldsig-more#sha384" => Self::Sha384,
"http://www.w3.org/2001/04/xmlenc#sha512" => Self::Sha512,
_ => Self::InvalidAlgorithm,
}
}
}
impl From<DigestAlgorithm> for String {
fn from(sa: DigestAlgorithm) -> String {
match sa {
DigestAlgorithm::Sha1 => String::from("http://www.w3.org/2000/09/xmldsig#sha1"),
DigestAlgorithm::Sha256 => String::from("http://www.w3.org/2001/04/xmlenc#sha256"),
DigestAlgorithm::Sha224 => {
String::from("http://www.w3.org/2001/04/xmldsig-more#sha224")
}
DigestAlgorithm::Sha384 => {
String::from("http://www.w3.org/2001/04/xmldsig-more#sha384")
}
DigestAlgorithm::Sha512 => String::from("http://www.w3.org/2001/04/xmlenc#sha512"),
_ => {
let result = format!("Invalid Algorithm specified: {:?}", sa);
log::error!("{}", result);
result
}
}
}
}
pub fn load_public_cert_from_filename(cert_filename: &str) -> Result<X509, String> {
log::debug!("loading cert: {}", cert_filename);
let mut f = match File::open(&cert_filename) {
Ok(value) => value,
Err(error) => {
return Err(format!(
"Error loading certificate file {}: {:?}",
&cert_filename, error
))
}
};
let mut cert_buffer = Vec::new();
match f.read_to_end(&mut cert_buffer) {
Ok(_) => eprintln!("Read certificate OK"),
Err(error) => {
return Err(format!("Failed to read cert? {:?}", error));
}
}
match X509::from_pem(&cert_buffer) {
Ok(value) => Ok(value),
Err(error) => Err(format!(
"Failed to load certificate from pem bytes: {:?}",
error
)),
}
}
impl DigestAlgorithm {
pub fn hash(
self,
bytes_to_hash: &[u8],
) -> Result<openssl::hash::DigestBytes, openssl::error::ErrorStack> {
match openssl::hash::hash(self.into(), bytes_to_hash) {
Ok(value) => {
debug!("Hashed bytes result: {:?}", value);
Ok(value)
}
Err(error) => {
eprintln!("Failed to hash bytes: {:?}", error);
Err(error)
}
}
}
}
pub fn sign_data(
signing_algorithm: crate::sign::SigningAlgorithm,
signing_key: &openssl::pkey::PKey<openssl::pkey::Private>,
bytes_to_sign: &[u8],
) -> Vec<u8> {
let signing_algorithm: openssl::hash::MessageDigest = signing_algorithm.into();
let mut signer = Signer::new(signing_algorithm, &signing_key).unwrap();
signer.update(bytes_to_sign).unwrap();
let signature = signer.sign_to_vec().unwrap();
log::debug!("Signature: {:?}", signature);
let mut verifier = Verifier::new(signing_algorithm, &signing_key).unwrap();
verifier.update(bytes_to_sign).unwrap();
assert!(verifier.verify(&signature).unwrap());
log::error!("Signed things, maybe?");
signature
}