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 cfg_if::cfg_if;
15 use std::ffi::CStr;
16
17 cfg_if! {
18 if #[cfg(any(ossl110, libressl271))] {
19 use ffi::{
20 OPENSSL_VERSION, OPENSSL_CFLAGS, OPENSSL_BUILT_ON, OPENSSL_PLATFORM, OPENSSL_DIR,
21 OpenSSL_version_num, OpenSSL_version,
22 };
23 } else {
24 use ffi::{
25 SSLEAY_VERSION as OPENSSL_VERSION, SSLEAY_CFLAGS as OPENSSL_CFLAGS,
26 SSLEAY_BUILT_ON as OPENSSL_BUILT_ON, SSLEAY_PLATFORM as OPENSSL_PLATFORM,
27 SSLEAY_DIR as OPENSSL_DIR, SSLeay as OpenSSL_version_num,
28 SSLeay_version as OpenSSL_version,
29 };
30 }
31 }
32
33 /// OPENSSL_VERSION_NUMBER is a numeric release version identifier:
34 ///
35 /// `MNNFFPPS: major minor fix patch status`
36 ///
37 /// The status nibble has one of the values 0 for development, 1 to e for betas 1 to 14, and f for release.
38 ///
39 /// for example
40 ///
41 /// `0x000906000 == 0.9.6 dev`
42 /// `0x000906023 == 0.9.6b beta 3`
43 /// `0x00090605f == 0.9.6e release`
44 ///
45 /// 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:
46 ///
47 /// `MMNNFFRBB major minor fix final beta/patch`
48 ///
49 /// for example
50 ///
51 /// `0x000904100 == 0.9.4 release`
52 /// `0x000905000 == 0.9.5 dev`
53 ///
54 /// 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
55 ///
56 /// 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() -> i6457 pub fn number() -> i64 {
58 unsafe { OpenSSL_version_num() as i64 }
59 }
60
61 /// The text variant of the version number and the release date. For example, "OpenSSL 0.9.5a 1 Apr 2000".
version() -> &'static str62 pub fn version() -> &'static str {
63 unsafe {
64 CStr::from_ptr(OpenSSL_version(OPENSSL_VERSION))
65 .to_str()
66 .unwrap()
67 }
68 }
69
70 /// The compiler flags set for the compilation process in the form "compiler: ..." if available or
71 /// "compiler: information not available" otherwise.
c_flags() -> &'static str72 pub fn c_flags() -> &'static str {
73 unsafe {
74 CStr::from_ptr(OpenSSL_version(OPENSSL_CFLAGS))
75 .to_str()
76 .unwrap()
77 }
78 }
79
80 /// The date of the build process in the form "built on: ..." if available or "built on: date not available" otherwise.
built_on() -> &'static str81 pub fn built_on() -> &'static str {
82 unsafe {
83 CStr::from_ptr(OpenSSL_version(OPENSSL_BUILT_ON))
84 .to_str()
85 .unwrap()
86 }
87 }
88
89 /// The "Configure" target of the library build in the form "platform: ..." if available or "platform: information not available" otherwise.
platform() -> &'static str90 pub fn platform() -> &'static str {
91 unsafe {
92 CStr::from_ptr(OpenSSL_version(OPENSSL_PLATFORM))
93 .to_str()
94 .unwrap()
95 }
96 }
97
98 /// The "OPENSSLDIR" setting of the library build in the form "OPENSSLDIR: "..."" if available or "OPENSSLDIR: N/A" otherwise.
dir() -> &'static str99 pub fn dir() -> &'static str {
100 unsafe {
101 CStr::from_ptr(OpenSSL_version(OPENSSL_DIR))
102 .to_str()
103 .unwrap()
104 }
105 }
106
107 /// This test ensures that we do not segfault when calling the functions of this module
108 /// and that the strings respect a reasonable format.
109 #[test]
test_versions()110 fn test_versions() {
111 println!("Number: '{}'", number());
112 println!("Version: '{}'", version());
113 println!("C flags: '{}'", c_flags());
114 println!("Built on: '{}'", built_on());
115 println!("Platform: '{}'", platform());
116 println!("Dir: '{}'", dir());
117
118 #[cfg(not(libressl))]
119 fn expected_name() -> &'static str {
120 "OpenSSL"
121 }
122 #[cfg(libressl)]
123 fn expected_name() -> &'static str {
124 "LibreSSL"
125 }
126
127 assert!(number() > 0);
128 assert!(version().starts_with(expected_name()));
129 assert!(c_flags().starts_with("compiler:"));
130 // some distributions patch out dates out of openssl so that the builds are reproducible
131 if !built_on().is_empty() {
132 assert!(built_on().starts_with("built on:"));
133 }
134 assert!(dir().starts_with("OPENSSLDIR:"));
135 }
136