From 1267e09ecb539395dfc769cc27bb94675b92c179 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sun, 13 Sep 2020 20:44:31 +0200 Subject: [PATCH] Dynamically retrieve the OpenSSL version --- acme_common/build.rs | 61 ++--------------------- acme_common/src/crypto.rs | 4 +- acme_common/src/crypto/openssl_version.rs | 27 ++++++++++ acmed/src/main.rs | 6 +-- 4 files changed, 35 insertions(+), 63 deletions(-) create mode 100644 acme_common/src/crypto/openssl_version.rs diff --git a/acme_common/build.rs b/acme_common/build.rs index b9b5f8d..d7d0d92 100644 --- a/acme_common/build.rs +++ b/acme_common/build.rs @@ -1,8 +1,4 @@ use std::env; -use std::fs::File; -use std::io::prelude::*; -use std::io::BufReader; -use std::path::PathBuf; macro_rules! set_rustc_env_var { ($name: expr, $value: expr) => {{ @@ -10,63 +6,12 @@ macro_rules! set_rustc_env_var { }}; } -fn get_openssl_version_unit(n: u64, pos: u32) -> u64 { - let p = 0x000f_f000_0000 >> (8 * pos); - let n = n & p; - n >> (8 * (3 - pos) + 4) -} -fn get_openssl_version(v: &str) -> String { - let v = u64::from_str_radix(&v, 16).unwrap(); - let mut version = vec![]; - for i in 0..3 { - let n = get_openssl_version_unit(v, i); - version.push(format!("{}", n)); - } - let version = version.join("."); - let p = get_openssl_version_unit(v, 3); - if p != 0 { - let p = p + 0x60; - let p = std::char::from_u32(p as u32).unwrap(); - format!("{}{}", version, p) - } else { - version - } -} - -fn get_lib_version(lib: &str) -> Option { - let pat = format!("\"checksum {} ", lib); - let mut lock_file = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); - lock_file.push("../Cargo.lock"); - let file = File::open(lock_file).unwrap(); - for line in BufReader::new(file).lines() { - let line = line.unwrap(); - if line.starts_with(&pat) { - let v: Vec<&str> = line.split(' ').collect(); - return Some(String::from(v[2])); - } - } - None -} - -fn set_tls() { - if let Ok(v) = env::var("DEP_OPENSSL_VERSION_NUMBER") { - let version = get_openssl_version(&v); - set_rustc_env_var!("ACMED_TLS_LIB_VERSION", version); +fn main() { + if let Ok(_) = env::var("DEP_OPENSSL_VERSION_NUMBER") { set_rustc_env_var!("ACMED_TLS_LIB_NAME", "OpenSSL"); } - if let Ok(v) = env::var("DEP_OPENSSL_LIBRESSL_VERSION_NUMBER") { - let version = get_openssl_version(&v); - set_rustc_env_var!("ACMED_TLS_LIB_VERSION", version); + if let Ok(_) = env::var("DEP_OPENSSL_LIBRESSL_VERSION_NUMBER") { set_rustc_env_var!("ACMED_TLS_LIB_NAME", "LibreSSL"); } - if env::var("CARGO_FEATURE_STANDALONE").is_ok() { - let version = get_lib_version("ring").unwrap(); - set_rustc_env_var!("ACMED_TLS_LIB_VERSION", version); - set_rustc_env_var!("ACMED_TLS_LIB_NAME", "ring"); - } -} - -fn main() { - set_tls(); } diff --git a/acme_common/src/crypto.rs b/acme_common/src/crypto.rs index 64f0fa8..6ddbf66 100644 --- a/acme_common/src/crypto.rs +++ b/acme_common/src/crypto.rs @@ -7,9 +7,8 @@ mod key_type; mod openssl_certificate; mod openssl_hash; mod openssl_keys; +mod openssl_version; -pub const TLS_LIB_NAME: &str = env!("ACMED_TLS_LIB_NAME"); -pub const TLS_LIB_VERSION: &str = env!("ACMED_TLS_LIB_VERSION"); pub const CRT_NB_DAYS_VALIDITY: u32 = 7; #[derive(Clone, Copy, Debug, PartialEq)] @@ -55,3 +54,4 @@ pub use key_type::KeyType; pub use openssl_certificate::{Csr, X509Certificate}; pub use openssl_hash::HashFunction; pub use openssl_keys::{gen_keypair, KeyPair}; +pub use openssl_version::{get_lib_name, get_lib_version}; diff --git a/acme_common/src/crypto/openssl_version.rs b/acme_common/src/crypto/openssl_version.rs new file mode 100644 index 0000000..6eb11d4 --- /dev/null +++ b/acme_common/src/crypto/openssl_version.rs @@ -0,0 +1,27 @@ +pub fn get_lib_name() -> String { + env!("ACMED_TLS_LIB_NAME").to_string() +} + +pub fn get_lib_version() -> String { + let v = openssl::version::number() as u64; + let mut version = vec![]; + for i in 0..3 { + let n = get_openssl_version_unit(v, i); + version.push(format!("{}", n)); + } + let version = version.join("."); + let p = get_openssl_version_unit(v, 3); + if p != 0 { + let p = p + 0x60; + let p = std::char::from_u32(p as u32).unwrap(); + format!("{}{}", version, p) + } else { + version + } +} + +fn get_openssl_version_unit(n: u64, pos: u32) -> u64 { + let p = 0x000f_f000_0000 >> (8 * pos); + let n = n & p; + n >> (8 * (3 - pos) + 4) +} diff --git a/acmed/src/main.rs b/acmed/src/main.rs index 12407fc..a0f5d1e 100644 --- a/acmed/src/main.rs +++ b/acmed/src/main.rs @@ -1,6 +1,6 @@ use crate::main_event_loop::MainEventLoop; use acme_common::crypto::{ - HashFunction, JwsSignatureAlgorithm, KeyType, TLS_LIB_NAME, TLS_LIB_VERSION, + HashFunction, JwsSignatureAlgorithm, KeyType, get_lib_name, get_lib_version }; use acme_common::logs::{set_log_system, DEFAULT_LOG_LEVEL}; use acme_common::{clean_pid_file, init_server}; @@ -52,8 +52,8 @@ fn main() { "{} {}\n\nCompiled with:\n {} {}\n {} {}", APP_VERSION, env!("ACMED_TARGET"), - TLS_LIB_NAME, - TLS_LIB_VERSION, + get_lib_name(), + get_lib_version(), env!("ACMED_HTTP_LIB_NAME"), env!("ACMED_HTTP_LIB_VERSION") );