|
| 1 | +use config::ProgramStartError; |
| 2 | +use openssl::ssl::{SslAcceptor, SslAcceptorBuilder, SslFiletype, SslMethod}; |
| 3 | +use std::fs::File; |
| 4 | +use std::io::{Error, Write}; |
| 5 | +use std::path::PathBuf; |
| 6 | +use tempdir::TempDir; |
| 7 | + |
| 8 | +const TMP_DIR_NAME: &'static str = "config-gen"; |
| 9 | + |
| 10 | +const TMP_KEY: &'static [u8] = include_bytes!("../key.pem"); |
| 11 | +const TMP_KEY_NAME: &'static str = "key.pem"; |
| 12 | + |
| 13 | +const TMP_CERT: &'static [u8] = include_bytes!("../cert.pem"); |
| 14 | +const TMP_CERT_NAME: &'static str = "cert.pem"; |
| 15 | + |
| 16 | +/// |
| 17 | +/// Create an SslAcceptorBuilder by using self-signed |
| 18 | +/// certificates that exist inside this binary |
| 19 | +/// |
| 20 | +/// This is acceptable since this is a development only |
| 21 | +/// tool and nothing this runs should be anywhere near anything |
| 22 | +/// that's shared, or in production. |
| 23 | +/// |
| 24 | +pub fn builder() -> Result<SslAcceptorBuilder, ProgramStartError> { |
| 25 | + let (key_path, cert_path, tmp_dir) = ssl_paths().map_err(|_e| ProgramStartError::SslTempDir)?; |
| 26 | + |
| 27 | + let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()) |
| 28 | + .map_err(|_e| ProgramStartError::SslFailed)?; |
| 29 | + |
| 30 | + builder |
| 31 | + .set_private_key_file(key_path, SslFiletype::PEM) |
| 32 | + .map_err(|_e| ProgramStartError::SslFailed)?; |
| 33 | + |
| 34 | + builder |
| 35 | + .set_certificate_chain_file(cert_path) |
| 36 | + .map_err(|_e| ProgramStartError::SslFailed)?; |
| 37 | + |
| 38 | + tmp_dir |
| 39 | + .close() |
| 40 | + .map_err(|_e| ProgramStartError::SslTempDirClose)?; |
| 41 | + |
| 42 | + Ok(builder) |
| 43 | +} |
| 44 | + |
| 45 | +#[test] |
| 46 | +fn test_ssl_builder() { |
| 47 | + builder().unwrap(); |
| 48 | +} |
| 49 | + |
| 50 | +/// |
| 51 | +/// Takes the self-signed bundled key & cert |
| 52 | +/// and places them in a temporary directory so that they |
| 53 | +/// can be used by openSSL |
| 54 | +/// |
| 55 | +/// # Examples |
| 56 | +/// |
| 57 | +/// ``` |
| 58 | +/// use bs::ssl::*; |
| 59 | +/// let (key_path, cert_path, tmp_dir) = ssl_paths().unwrap(); |
| 60 | +/// println!("key={:?}, cert={:?}", key_path, cert_path); |
| 61 | +/// tmp_dir.close().unwrap(); |
| 62 | +/// ``` |
| 63 | +/// |
| 64 | +pub fn ssl_paths() -> Result<(PathBuf, PathBuf, TempDir), Error> { |
| 65 | + let tmp_dir = TempDir::new(TMP_DIR_NAME)?; |
| 66 | + let key_path = tmp_dir.path().join(TMP_KEY_NAME); |
| 67 | + let cert_path = tmp_dir.path().join(TMP_CERT_NAME); |
| 68 | + |
| 69 | + let mut key_file = File::create(&key_path)?; |
| 70 | + key_file.write_all(TMP_KEY)?; |
| 71 | + key_file.sync_all()?; |
| 72 | + |
| 73 | + let mut cert_file = File::create(&cert_path)?; |
| 74 | + cert_file.write_all(TMP_CERT)?; |
| 75 | + cert_file.sync_all()?; |
| 76 | + |
| 77 | + Ok((key_path, cert_path, tmp_dir)) |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn test_ssl_paths() { |
| 82 | + let (_file_key, _file_cert, tmp_dir) = ssl_paths().unwrap(); |
| 83 | + assert_eq!(tmp_dir.path().exists(), true); |
| 84 | +} |
0 commit comments