File: ssl_cert_blob.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (63 lines) | stat: -rw-r--r-- 1,437 bytes parent folder | download | duplicates (5)
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
use std::env;
use std::fs::File;
use std::io::{stdout, Read, Write};
use std::path::Path;

use anyhow::{bail, Result};
use curl::easy::Easy;

fn read_file(path: impl AsRef<Path>) -> Result<Vec<u8>> {
    let mut f = File::open(path)?;
    let mut buf = Vec::new();
    f.read_to_end(&mut buf)?;
    Ok(buf)
}

fn main() -> Result<()> {
    let argv = env::args().collect::<Vec<_>>();
    if argv.len() < 4 {
        bail!("usage: ssl_cert_blob URL CERT KEY CAINFO? PASSWORD?");
    }
    let url = &argv[1];
    let cert_path = &argv[2];
    let key_path = &argv[3];
    let cainfo = if argv.len() >= 5 {
        Some(&argv[4])
    } else {
        None
    };
    let password = if argv.len() >= 6 {
        Some(&argv[5])
    } else {
        None
    };

    let mut handle = Easy::new();

    handle.url(url)?;
    handle.verbose(true)?;
    handle.write_function(|data| {
        stdout().write_all(data).unwrap();
        Ok(data.len())
    })?;

    let cert_blob = read_file(cert_path)?;
    let key_blob = read_file(key_path)?;
    let ca_blob = if let Some(cainfo) = cainfo {
        Some(read_file(cainfo)?)
    } else {
        None
    };

    handle.ssl_cert_blob(&cert_blob)?;
    handle.ssl_key_blob(&key_blob)?;
    if let Some(password) = password {
        handle.key_password(password)?;
    }
    if let Some(ca_blob) = ca_blob {
        handle.ssl_cainfo_blob(&ca_blob)?;
    }

    handle.perform()?;
    Ok(())
}