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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use clap::{CommandFactory, Parser, Subcommand};
use std::process;
mod cert;
mod commands;
mod output;
#[derive(Parser)]
#[command(
name = "sslx",
about = "The modern way to work with certificates and TLS",
version,
after_help = "Examples:\n \
sslx inspect cert.pem Read a certificate file\n \
sslx connect google.com Test TLS connection\n \
sslx verify cert.pem --ca ca Validate certificate chain\n \
sslx generate --cn localhost Create a self-signed cert"
)]
struct Cli {
#[command(subcommand)]
command: Commands,
/// Output as JSON
#[arg(long, global = true)]
json: bool,
/// Disable colored output
#[arg(long, global = true)]
no_color: bool,
}
#[derive(Subcommand)]
enum Commands {
/// Read and display certificate file details
Inspect {
/// Path to certificate file (PEM, DER, or PKCS12)
file: String,
},
/// Test TLS connection and show certificate chain
Connect {
/// Hostname or host:port to connect to
host: String,
/// Override SNI hostname
#[arg(long)]
sni: Option<String>,
/// ALPN protocols (comma-separated)
#[arg(long)]
alpn: Option<String>,
/// Connection timeout in seconds
#[arg(long, default_value = "10")]
timeout: u64,
/// Skip certificate verification (still shows issues)
#[arg(long)]
insecure: bool,
},
/// Validate certificate against CA bundle
Verify {
/// Path to certificate to verify
cert: String,
/// Path to CA bundle or certificate
#[arg(long)]
ca: String,
},
/// Generate a self-signed certificate for local development
Generate {
/// Common Name
#[arg(long, default_value = "localhost")]
cn: String,
/// Subject Alternative Names (comma-separated)
#[arg(long, value_delimiter = ',')]
san: Vec<String>,
/// Validity period in days
#[arg(long, default_value = "365")]
days: u32,
/// Key type: ec256 (default), ec384, ed25519
#[arg(long, default_value = "ec256")]
key_type: String,
/// Output directory
#[arg(short, long, default_value = ".")]
out: String,
},
/// Convert between certificate formats (PEM, DER, PKCS12)
Convert {
/// Input file path
input: String,
/// Target format: pem, der, pkcs12
#[arg(long)]
to: String,
/// Private key file (required for PKCS12 export)
#[arg(long)]
key: Option<String>,
/// Password for PKCS12
#[arg(long)]
password: Option<String>,
/// Output file path (auto-generated if omitted)
#[arg(short, long)]
out: Option<String>,
},
/// Check if a certificate and private key match
#[command(name = "match")]
Match {
/// Path to certificate file
cert: String,
/// Path to private key file
key: String,
},
/// Extract cert, key, and chain from a PKCS12 file
Extract {
/// Path to .p12/.pfx file
input: String,
/// PKCS12 password
#[arg(long)]
password: Option<String>,
/// Output directory
#[arg(short, long, default_value = ".")]
out: String,
},
/// Generate a Certificate Signing Request (CSR)
Csr {
/// Common Name
#[arg(long)]
cn: String,
/// Subject Alternative Names (comma-separated)
#[arg(long, value_delimiter = ',')]
san: Vec<String>,
/// Key type: ec256 (default), ec384, ed25519
#[arg(long, default_value = "ec256")]
key_type: String,
/// Output directory
#[arg(short, long, default_value = ".")]
out: String,
},
/// Auto-detect and decode any crypto file (cert, key, CSR, JWT)
Decode {
/// File path or inline string (e.g., JWT token)
input: String,
},
/// TLS security grade (A+ to F) like SSL Labs
Grade {
/// Hostname or host:port
host: String,
},
/// Check certificate expiry across multiple hosts
Expiry {
/// One or more hostnames (or host:port)
hosts: Vec<String>,
},
/// Generate shell completions
#[command(hide = true)]
Completions {
/// Shell type: bash, zsh, fish, powershell
shell: clap_complete::Shell,
},
/// Generate man page
#[command(hide = true)]
Manpage,
}
fn main() {
let cli = Cli::parse();
let exit_code = match run(cli) {
Ok(code) => code,
Err(e) => {
let use_color = output::colors::should_color();
if use_color {
eprintln!(
"\n {}{}error:{} {}",
output::colors::BOLD_RED,
output::box_chars::CROSS,
output::colors::RESET,
e
);
} else {
eprintln!("\n error: {}", e);
}
// Print cause chain
let mut source: Option<&dyn std::error::Error> = e.source();
while let Some(cause) = source {
if use_color {
eprintln!(
" {}{}{}",
output::colors::DIM,
cause,
output::colors::RESET
);
} else {
eprintln!(" {}", cause);
}
source = cause.source();
}
eprintln!();
10 // usage/general error
}
};
process::exit(exit_code);
}
fn run(cli: Cli) -> anyhow::Result<i32> {
match cli.command {
Commands::Inspect { file } => commands::inspect::run(&file, cli.json, cli.no_color),
Commands::Connect {
host,
sni,
alpn,
timeout,
insecure,
} => {
let (host, port) = parse_host_port(&host);
let alpn_protos: Option<Vec<String>> =
alpn.map(|a| a.split(',').map(|s| s.trim().to_string()).collect());
commands::connect::run(commands::connect::ConnectOptions {
host: &host,
port,
sni: sni.as_deref(),
alpn: alpn_protos.as_deref(),
timeout,
insecure,
json: cli.json,
no_color: cli.no_color,
})
}
Commands::Verify { cert, ca } => commands::verify::run(&cert, &ca, cli.json, cli.no_color),
Commands::Generate {
cn,
san,
days,
key_type,
out,
} => commands::generate::run(&cn, &san, days, &key_type, &out, cli.json, cli.no_color),
Commands::Convert {
input,
to,
key,
password,
out,
} => commands::convert::run(
&input,
&to,
key.as_deref(),
password.as_deref(),
out.as_deref(),
cli.json,
cli.no_color,
),
Commands::Match { cert, key } => {
commands::match_cmd::run(&cert, &key, cli.json, cli.no_color)
}
Commands::Extract {
input,
password,
out,
} => commands::extract::run(&input, password.as_deref(), &out, cli.json, cli.no_color),
Commands::Csr {
cn,
san,
key_type,
out,
} => commands::csr::run(&cn, &san, &key_type, &out, cli.json, cli.no_color),
Commands::Decode { input } => commands::decode::run(&input, cli.json, cli.no_color),
Commands::Grade { host } => {
let (host, port) = parse_host_port(&host);
commands::grade::run(&host, port, cli.json, cli.no_color)
}
Commands::Expiry { hosts } => commands::expiry::run(&hosts, cli.json, cli.no_color),
Commands::Completions { shell } => {
clap_complete::generate(shell, &mut Cli::command(), "sslx", &mut std::io::stdout());
Ok(0)
}
Commands::Manpage => {
let cmd = Cli::command();
let man = clap_mangen::Man::new(cmd);
man.render(&mut std::io::stdout())?;
Ok(0)
}
}
}
/// Parse "host:port" or just "host" (defaults to 443)
fn parse_host_port(input: &str) -> (String, u16) {
if let Some((host, port_str)) = input.rsplit_once(':') {
if let Ok(port) = port_str.parse::<u16>() {
return (host.to_string(), port);
}
}
(input.to_string(), 443)
}