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 use std::ffi::CStr;
15 
16 cfg_if! {
17     if #[cfg(any(ossl110, libressl271))] {
18         use ffi::{
19             OPENSSL_VERSION, OPENSSL_CFLAGS, OPENSSL_BUILT_ON, OPENSSL_PLATFORM, OPENSSL_DIR,
20             OpenSSL_version_num, OpenSSL_version,
21         };
22     } else {
23         use ffi::{
24             SSLEAY_VERSION as OPENSSL_VERSION, SSLEAY_CFLAGS as OPENSSL_CFLAGS,
25             SSLEAY_BUILT_ON as OPENSSL_BUILT_ON, SSLEAY_PLATFORM as OPENSSL_PLATFORM,
26             SSLEAY_DIR as OPENSSL_DIR, SSLeay as OpenSSL_version_num,
27             SSLeay_version as OpenSSL_version,
28         };
29     }
30 }
31 
32 /// OPENSSL_VERSION_NUMBER is a numeric release version identifier:
33 ///
34 /// `MNNFFPPS: major minor fix patch status`
35 ///
36 /// The status nibble has one of the values 0 for development, 1 to e for betas 1 to 14, and f for release.
37 ///
38 /// for example
39 ///
40 /// `0x000906000 == 0.9.6 dev`
41 /// `0x000906023 == 0.9.6b beta 3`
42 /// `0x00090605f == 0.9.6e release`
43 ///
44 /// Versions prior to 0.9.3 have identifiers < 0x0930. Versions between 0.9.3 and 0.9.5 had a version identifier with this interpretation:
45 ///
46 /// `MMNNFFRBB major minor fix final beta/patch`
47 ///
48 /// for example
49 ///
50 /// `0x000904100 == 0.9.4 release`
51 /// `0x000905000 == 0.9.5 dev`
52 ///
53 /// Version 0.9.5a had an interim interpretation that is like the current one, except the patch level got the highest bit set, to keep continuity. The number was therefore 0x0090581f
54 ///
55 /// The return value of this function can be compared to the macro to make sure that the correct version of the library has been loaded, especially when using DLLs on Windows systems.
number() -> i6456 pub fn number() -> i64 {
57     unsafe { OpenSSL_version_num() as i64 }
58 }
59 
60 /// The text variant of the version number and the release date. For example, "OpenSSL 0.9.5a 1 Apr 2000".
version() -> &'static str61 pub fn version() -> &'static str {
62     unsafe {
63         CStr::from_ptr(OpenSSL_version(OPENSSL_VERSION))
64             .to_str()
65             .unwrap()
66     }
67 }
68 
69 /// The compiler flags set for the compilation process in the form "compiler: ..." if available or
70 /// "compiler: information not available" otherwise.
c_flags() -> &'static str71 pub fn c_flags() -> &'static str {
72     unsafe {
73         CStr::from_ptr(OpenSSL_version(OPENSSL_CFLAGS))
74             .to_str()
75             .unwrap()
76     }
77 }
78 
79 /// The date of the build process in the form "built on: ..." if available or "built on: date not available" otherwise.
built_on() -> &'static str80 pub fn built_on() -> &'static str {
81     unsafe {
82         CStr::from_ptr(OpenSSL_version(OPENSSL_BUILT_ON))
83             .to_str()
84             .unwrap()
85     }
86 }
87 
88 /// The "Configure" target of the library build in the form "platform: ..." if available or "platform: information not available" otherwise.
platform() -> &'static str89 pub fn platform() -> &'static str {
90     unsafe {
91         CStr::from_ptr(OpenSSL_version(OPENSSL_PLATFORM))
92             .to_str()
93             .unwrap()
94     }
95 }
96 
97 /// The "OPENSSLDIR" setting of the library build in the form "OPENSSLDIR: "..."" if available or "OPENSSLDIR: N/A" otherwise.
dir() -> &'static str98 pub fn dir() -> &'static str {
99     unsafe {
100         CStr::from_ptr(OpenSSL_version(OPENSSL_DIR))
101             .to_str()
102             .unwrap()
103     }
104 }
105 
106 /// This test ensures that we do not segfault when calling the functions of this module
107 /// and that the strings respect a reasonable format.
108 #[test]
test_versions()109 fn test_versions() {
110     println!("Number: '{}'", number());
111     println!("Version: '{}'", version());
112     println!("C flags: '{}'", c_flags());
113     println!("Built on: '{}'", built_on());
114     println!("Platform: '{}'", platform());
115     println!("Dir: '{}'", dir());
116 
117     #[cfg(not(libressl))]
118     fn expected_name() -> &'static str {
119         "OpenSSL"
120     }
121     #[cfg(libressl)]
122     fn expected_name() -> &'static str {
123         "LibreSSL"
124     }
125 
126     assert!(number() > 0);
127     assert!(version().starts_with(expected_name()));
128     assert!(c_flags().starts_with("compiler:"));
129     assert!(built_on().starts_with("built on:"));
130     assert!(dir().starts_with("OPENSSLDIR:"));
131 }
132