Skip to main content

variant_ssl/
version.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13
14//! Build and version information.
15
16use openssl_macros::corresponds;
17use std::ffi::CStr;
18
19use ffi::{
20    OpenSSL_version, OpenSSL_version_num, OPENSSL_BUILT_ON, OPENSSL_CFLAGS, OPENSSL_DIR,
21    OPENSSL_PLATFORM, OPENSSL_VERSION,
22};
23
24/// OPENSSL_VERSION_NUMBER is a numeric release version identifier:
25///
26/// `MNNFFPPS: major minor fix patch status`
27///
28/// The status nibble has one of the values 0 for development, 1 to e for betas 1 to 14, and f for release.
29///
30/// for example
31///
32/// `0x000906000 == 0.9.6 dev`
33/// `0x000906023 == 0.9.6b beta 3`
34/// `0x00090605f == 0.9.6e release`
35#[corresponds(OpenSSL_version_num)]
36pub fn number() -> i64 {
37    unsafe { OpenSSL_version_num() as i64 }
38}
39
40/// The text variant of the version number and the release date. For example, "OpenSSL 0.9.5a 1 Apr 2000".
41#[corresponds(OpenSSL_version)]
42pub fn version() -> &'static str {
43    unsafe {
44        CStr::from_ptr(OpenSSL_version(OPENSSL_VERSION))
45            .to_str()
46            .unwrap()
47    }
48}
49
50/// The compiler flags set for the compilation process in the form "compiler: ..." if available or
51/// "compiler: information not available" otherwise.
52#[corresponds(OpenSSL_version)]
53pub fn c_flags() -> &'static str {
54    unsafe {
55        CStr::from_ptr(OpenSSL_version(OPENSSL_CFLAGS))
56            .to_str()
57            .unwrap()
58    }
59}
60
61/// The date of the build process in the form "built on: ..." if available or "built on: date not available" otherwise.
62#[corresponds(OpenSSL_version)]
63pub fn built_on() -> &'static str {
64    unsafe {
65        CStr::from_ptr(OpenSSL_version(OPENSSL_BUILT_ON))
66            .to_str()
67            .unwrap()
68    }
69}
70
71/// The "Configure" target of the library build in the form "platform: ..." if available or "platform: information not available" otherwise.
72#[corresponds(OpenSSL_version)]
73pub fn platform() -> &'static str {
74    unsafe {
75        CStr::from_ptr(OpenSSL_version(OPENSSL_PLATFORM))
76            .to_str()
77            .unwrap()
78    }
79}
80
81/// The "OPENSSLDIR" setting of the library build in the form "OPENSSLDIR: "..."" if available or "OPENSSLDIR: N/A" otherwise.
82#[corresponds(OpenSSL_version)]
83pub fn dir() -> &'static str {
84    unsafe {
85        CStr::from_ptr(OpenSSL_version(OPENSSL_DIR))
86            .to_str()
87            .unwrap()
88    }
89}
90
91/// This test ensures that we do not segfault when calling the functions of this module
92/// and that the strings respect a reasonable format.
93#[test]
94fn test_versions() {
95    println!("Number: '{}'", number());
96    println!("Version: '{}'", version());
97    println!("C flags: '{}'", c_flags());
98    println!("Built on: '{}'", built_on());
99    println!("Platform: '{}'", platform());
100    println!("Dir: '{}'", dir());
101
102    #[cfg(not(any(libressl, boringssl, awslc)))]
103    fn expected_name() -> &'static str {
104        "OpenSSL"
105    }
106    #[cfg(libressl)]
107    fn expected_name() -> &'static str {
108        "LibreSSL"
109    }
110    #[cfg(boringssl)]
111    fn expected_name() -> &'static str {
112        "BoringSSL"
113    }
114    #[cfg(awslc)]
115    fn expected_name() -> &'static str {
116        "AWS-LC"
117    }
118
119    assert!(number() > 0);
120    assert!(version().starts_with(expected_name()));
121    assert!(c_flags().starts_with("compiler:"));
122    // some distributions patch out dates out of openssl so that the builds are reproducible
123    if !built_on().is_empty() {
124        assert!(built_on().starts_with("built on:"));
125    }
126}