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
use std::ptr;
use std::ffi::{CString};
use std::os::raw::{c_char};
use ffi;
#[derive(Debug)]
pub struct SslOptions {
pub copts: ffi::MQTTAsync_SSLOptions,
trust_store: CString,
key_store: CString,
private_key: CString,
private_key_password: CString,
enabled_cipher_suites: CString,
}
impl SslOptions {
pub fn new() -> SslOptions {
let opts = SslOptions {
copts: ffi::MQTTAsync_SSLOptions::default(),
trust_store: CString::new("").unwrap(),
key_store: CString::new("").unwrap(),
private_key: CString::new("").unwrap(),
private_key_password: CString::new("").unwrap(),
enabled_cipher_suites: CString::new("").unwrap(),
};
SslOptions::fixup(opts)
}
fn c_str(str: &CString) -> *const c_char {
if str.to_bytes().len() == 0 { ptr::null() } else { str.as_ptr() }
}
fn fixup(mut opts: SslOptions) -> SslOptions {
opts.copts.trustStore = SslOptions::c_str(&opts.trust_store);
opts.copts.keyStore = SslOptions::c_str(&opts.key_store);
opts.copts.privateKey = SslOptions::c_str(&opts.private_key);
opts.copts.privateKeyPassword = SslOptions::c_str(&opts.private_key_password);
opts.copts.enabledCipherSuites = SslOptions::c_str(&opts.enabled_cipher_suites);
opts
}
pub fn get_trust_store(&self) -> String {
self.trust_store.to_str().unwrap().to_string()
}
pub fn get_key_store(&self) -> String {
self.key_store.to_str().unwrap().to_string()
}
pub fn get_private_key(&self) -> String {
self.private_key.to_str().unwrap().to_string()
}
pub fn get_private_key_password(&self) -> String {
self.private_key_password.to_str().unwrap().to_string()
}
pub fn get_enabled_cipher_suites(&self) -> String {
self.enabled_cipher_suites.to_str().unwrap().to_string()
}
pub fn get_enable_server_cert_auth(&self) -> bool {
self.copts.enableServerCertAuth != 0
}
}
impl Clone for SslOptions {
fn clone(&self) -> SslOptions {
let ssl = SslOptions {
copts: self.copts.clone(),
trust_store: self.trust_store.clone(),
key_store: self.key_store.clone(),
private_key: self.private_key.clone(),
private_key_password: self.private_key_password.clone(),
enabled_cipher_suites: self.enabled_cipher_suites.clone(),
};
SslOptions::fixup(ssl)
}
}
#[derive(Debug)]
pub struct SslOptionsBuilder {
trust_store: String,
key_store: String,
private_key: String,
private_key_password: String,
enabled_cipher_suites: String,
enable_server_cert_auth: bool,
}
impl SslOptionsBuilder {
pub fn new() -> SslOptionsBuilder {
SslOptionsBuilder {
trust_store: "".to_string(),
key_store: "".to_string(),
private_key: "".to_string(),
private_key_password: "".to_string(),
enabled_cipher_suites: "".to_string(),
enable_server_cert_auth: true,
}
}
pub fn trust_store(&mut self, trust_store: &str) -> &mut SslOptionsBuilder {
self.trust_store = trust_store.to_string();
self
}
pub fn key_store(&mut self, key_store: &str) -> &mut SslOptionsBuilder {
self.key_store = key_store.to_string();
self
}
pub fn private_key(&mut self, private_key: &str) -> &mut SslOptionsBuilder {
self.private_key = private_key.to_string();
self
}
pub fn private_key_password(&mut self, private_key_password: &str) -> &mut SslOptionsBuilder {
self.private_key_password = private_key_password.to_string();
self
}
pub fn enabled_cipher_suites(&mut self, enabled_cipher_suites: &str) -> &mut SslOptionsBuilder {
self.enabled_cipher_suites = enabled_cipher_suites.to_string();
self
}
pub fn finalize(&self) -> SslOptions {
let mut opts = SslOptions {
copts: ffi::MQTTAsync_SSLOptions::default(),
trust_store: CString::new(self.trust_store.clone()).unwrap(),
key_store: CString::new(self.key_store.clone()).unwrap(),
private_key: CString::new(self.private_key.clone()).unwrap(),
private_key_password: CString::new(self.private_key_password.clone()).unwrap(),
enabled_cipher_suites: CString::new(self.enabled_cipher_suites.clone()).unwrap(),
};
opts.copts.enableServerCertAuth = if self.enable_server_cert_auth { 1 } else { 0 };
SslOptions::fixup(opts)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::{CStr};
#[test]
fn test_new() {
let opts = SslOptions::new();
assert_eq!([ 'M' as i8, 'Q' as i8, 'T' as i8, 'S' as i8 ], opts.copts.struct_id);
assert_eq!(0, opts.copts.struct_version);
assert_eq!(ptr::null(), opts.copts.trustStore);
}
#[test]
fn test_builder_dflt() {
let opts = SslOptionsBuilder::new()
.finalize();
assert_eq!([ 'M' as i8, 'Q' as i8, 'T' as i8, 'S' as i8 ], opts.copts.struct_id);
assert_eq!(0, opts.copts.struct_version);
assert_eq!(ptr::null(), opts.copts.trustStore);
}
#[test]
fn test_builder_trust_store() {
const TRUST_STORE: &str = "some_file.crt";
let opts = SslOptionsBuilder::new()
.trust_store(TRUST_STORE)
.finalize();
assert_eq!(TRUST_STORE, opts.trust_store.to_str().unwrap());
let ts = unsafe { CStr::from_ptr(opts.copts.trustStore) };
assert_eq!(TRUST_STORE, ts.to_str().unwrap());
}
#[test]
fn test_builder_key_store() {
const KEY_STORE: &str = "some_file.crt";
let opts = SslOptionsBuilder::new()
.key_store(KEY_STORE)
.finalize();
assert_eq!(KEY_STORE, opts.key_store.to_str().unwrap());
}
#[test]
fn test_copy() {
const TRUST_STORE: &str = "some_file.crt";
let org_opts = SslOptionsBuilder::new()
.trust_store(TRUST_STORE)
.finalize();
let opts = org_opts;
assert_eq!(TRUST_STORE, opts.trust_store.to_str().unwrap());
let ts = unsafe { CStr::from_ptr(opts.copts.trustStore) };
assert_eq!(TRUST_STORE, ts.to_str().unwrap());
}
#[test]
fn test_clone() {
const TRUST_STORE: &str = "some_file.crt";
let opts = {
let org_opts = SslOptionsBuilder::new()
.trust_store(TRUST_STORE)
.finalize();
org_opts.clone()
};
assert_eq!(TRUST_STORE, opts.trust_store.to_str().unwrap());
let ts = unsafe { CStr::from_ptr(opts.copts.trustStore) };
assert_eq!(TRUST_STORE, ts.to_str().unwrap());
}
}