Browse Source

Dynamically retrieve the OpenSSL version

pull/39/head
Rodolphe Breard 4 years ago
parent
commit
1267e09ecb
  1. 61
      acme_common/build.rs
  2. 4
      acme_common/src/crypto.rs
  3. 27
      acme_common/src/crypto/openssl_version.rs
  4. 6
      acmed/src/main.rs

61
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<String> {
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();
}

4
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};

27
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)
}

6
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")
);

Loading…
Cancel
Save