Skip to content

Commit 4722f97

Browse files
authored
Merge pull request #1264 from sfackler/openssl-300
Initial support for OpenSSL 3.0.0-alpha17
2 parents ae494cc + 9434332 commit 4722f97

38 files changed

+1296
-645
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ jobs:
147147
library:
148148
- name: openssl
149149
version: vendored
150+
- name: openssl
151+
version: 3.0.0-beta1
152+
dl-path: /
150153
- name: openssl
151154
version: 1.1.1k
152155
dl-path: /

openssl-errors/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ readme = "README.md"
1010
categories = ["api-bindings"]
1111

1212
[dependencies]
13+
cfg-if = "0.1"
1314
libc = "0.2"
1415

1516
openssl-sys = { version = "0.9.42", path = "../openssl-sys" }

openssl-errors/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![allow(clippy::inconsistent_digit_grouping, clippy::unusual_byte_groupings)]
2+
3+
use std::env;
4+
5+
fn main() {
6+
if let Ok(version) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
7+
let version = u64::from_str_radix(&version, 16).unwrap();
8+
9+
if version >= 0x3_00_00_00_0 {
10+
println!("cargo:rustc-cfg=ossl300");
11+
}
12+
}
13+
}

openssl-errors/src/lib.rs

Lines changed: 146 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#![warn(missing_docs)]
4848
#![doc(html_root_url = "https://docs.rs/openssl-errors/0.1")]
4949

50+
use cfg_if::cfg_if;
5051
use libc::{c_char, c_int};
5152
use std::borrow::Cow;
5253
use std::marker::PhantomData;
@@ -70,19 +71,37 @@ pub trait Library {
7071
fn id() -> c_int;
7172
}
7273

74+
cfg_if! {
75+
if #[cfg(ossl300)] {
76+
type FunctionInner = *const c_char;
77+
} else {
78+
type FunctionInner = c_int;
79+
}
80+
}
81+
7382
/// A function declaration, parameterized by its error library.
74-
pub struct Function<T>(c_int, PhantomData<T>);
83+
pub struct Function<T>(FunctionInner, PhantomData<T>);
84+
85+
// manual impls necessary for the 3.0.0 case
86+
unsafe impl<T> Sync for Function<T> where T: Sync {}
87+
unsafe impl<T> Send for Function<T> where T: Send {}
7588

7689
impl<T> Function<T> {
77-
/// Creates a function from its raw identifier.
90+
/// This is not considered a part of the crate's public API, and is subject to change at any time.
91+
///
92+
/// # Safety
93+
///
94+
/// The inner value must be valid for the lifetime of the process.
95+
#[doc(hidden)]
7896
#[inline]
79-
pub const fn from_raw(raw: c_int) -> Function<T> {
97+
pub const unsafe fn __from_raw(raw: FunctionInner) -> Function<T> {
8098
Function(raw, PhantomData)
8199
}
82100

83-
/// Returns the function's raw identifier.
101+
/// This is not considered a part of the crate's public API, and is subject to change at any time.
102+
#[doc(hidden)]
84103
#[inline]
85-
pub const fn as_raw(&self) -> c_int {
104+
pub const fn __as_raw(&self) -> FunctionInner {
86105
self.0
87106
}
88107
}
@@ -91,15 +110,17 @@ impl<T> Function<T> {
91110
pub struct Reason<T>(c_int, PhantomData<T>);
92111

93112
impl<T> Reason<T> {
94-
/// Creates a reason from its raw identifier.
113+
/// This is not considered a part of the crate's public API, and is subject to change at any time.
114+
#[doc(hidden)]
95115
#[inline]
96-
pub const fn from_raw(raw: c_int) -> Reason<T> {
116+
pub const fn __from_raw(raw: c_int) -> Reason<T> {
97117
Reason(raw, PhantomData)
98118
}
99119

100-
/// Returns the reason's raw identifier.
120+
/// This is not considered a part of the crate's public API, and is subject to change at any time.
121+
#[doc(hidden)]
101122
#[inline]
102-
pub const fn as_raw(&self) -> c_int {
123+
pub const fn __as_raw(&self) -> c_int {
103124
self.0
104125
}
105126
}
@@ -119,13 +140,37 @@ pub unsafe fn __put_error<T>(
119140
) where
120141
T: Library,
121142
{
122-
openssl_sys::ERR_put_error(
123-
T::id(),
124-
func.as_raw(),
125-
reason.as_raw(),
126-
file.as_ptr() as *const c_char,
127-
line as c_int,
128-
);
143+
put_error_inner(T::id(), func.0, reason.0, file, line, message)
144+
}
145+
146+
unsafe fn put_error_inner(
147+
library: c_int,
148+
func: FunctionInner,
149+
reason: c_int,
150+
file: &'static str,
151+
line: u32,
152+
message: Option<Cow<'static, str>>,
153+
) {
154+
cfg_if! {
155+
if #[cfg(ossl300)] {
156+
openssl_sys::ERR_new();
157+
openssl_sys::ERR_set_debug(
158+
file.as_ptr() as *const c_char,
159+
line as c_int,
160+
func,
161+
);
162+
openssl_sys::ERR_set_error(library, reason, ptr::null());
163+
} else {
164+
openssl_sys::ERR_put_error(
165+
library,
166+
func,
167+
reason,
168+
file.as_ptr() as *const c_char,
169+
line as c_int,
170+
);
171+
}
172+
}
173+
129174
let data = match message {
130175
Some(Cow::Borrowed(s)) => Some((s.as_ptr() as *const c_char as *mut c_char, 0)),
131176
Some(Cow::Owned(s)) => {
@@ -223,31 +268,11 @@ macro_rules! openssl_errors {
223268
fn id() -> $crate::export::c_int {
224269
static INIT: $crate::export::Once = $crate::export::Once::new();
225270
static mut LIB_NUM: $crate::export::c_int = 0;
226-
static mut STRINGS: [
227-
$crate::export::ERR_STRING_DATA;
228-
2 + $crate::openssl_errors!(@count $($func_name;)* $($reason_name;)*)
229-
] = [
230-
$crate::export::ERR_STRING_DATA {
231-
error: 0,
232-
string: concat!($lib_str, "\0").as_ptr() as *const $crate::export::c_char,
233-
},
234-
$(
235-
$crate::export::ERR_STRING_DATA {
236-
error: $crate::export::ERR_PACK(0, $lib_name::$func_name.as_raw(), 0),
237-
string: concat!($func_str, "\0").as_ptr() as *const $crate::export::c_char,
238-
},
239-
)*
240-
$(
241-
$crate::export::ERR_STRING_DATA {
242-
error: $crate::export::ERR_PACK(0, 0, $lib_name::$reason_name.as_raw()),
243-
string: concat!($reason_str, "\0").as_ptr() as *const $crate::export::c_char,
244-
},
245-
)*
246-
$crate::export::ERR_STRING_DATA {
247-
error: 0,
248-
string: $crate::export::null(),
249-
}
250-
];
271+
$crate::__openssl_errors_helper! {
272+
@strings $lib_name($lib_str)
273+
functions { $($func_name($func_str);)* }
274+
reasons { $($reason_name($reason_str);)* }
275+
}
251276

252277
unsafe {
253278
INIT.call_once(|| {
@@ -263,19 +288,21 @@ macro_rules! openssl_errors {
263288
}
264289

265290
impl $lib_name {
266-
$crate::openssl_errors!(@func_consts $lib_name; 1; $($(#[$func_attr])* $func_name;)*);
291+
$crate::openssl_errors!(@func_consts $lib_name; 1; $($(#[$func_attr])* $func_name($func_str);)*);
267292
$crate::openssl_errors!(@reason_consts $lib_name; 1; $($(#[$reason_attr])* $reason_name;)*);
268293
}
269294
)*};
270-
(@func_consts $lib_name:ident; $n:expr; $(#[$attr:meta])* $name:ident; $($tt:tt)*) => {
295+
(@func_consts $lib_name:ident; $n:expr; $(#[$attr:meta])* $name:ident($str:expr); $($tt:tt)*) => {
271296
$(#[$attr])*
272-
pub const $name: $crate::Function<$lib_name> = $crate::Function::from_raw($n);
297+
pub const $name: $crate::Function<$lib_name> = unsafe {
298+
$crate::Function::__from_raw($crate::__openssl_errors_helper!(@func_value $n, $str))
299+
};
273300
$crate::openssl_errors!(@func_consts $lib_name; $n + 1; $($tt)*);
274301
};
275302
(@func_consts $lib_name:ident; $n:expr;) => {};
276303
(@reason_consts $lib_name:ident; $n:expr; $(#[$attr:meta])* $name:ident; $($tt:tt)*) => {
277304
$(#[$attr])*
278-
pub const $name: $crate::Reason<$lib_name> = $crate::Reason::from_raw($n);
305+
pub const $name: $crate::Reason<$lib_name> = $crate::Reason::__from_raw($n);
279306
$crate::openssl_errors!(@reason_consts $lib_name; $n + 1; $($tt)*);
280307
};
281308
(@reason_consts $lib_name:ident; $n:expr;) => {};
@@ -284,3 +311,77 @@ macro_rules! openssl_errors {
284311
};
285312
(@count) => { 0 };
286313
}
314+
315+
cfg_if! {
316+
if #[cfg(ossl300)] {
317+
#[doc(hidden)]
318+
#[macro_export]
319+
macro_rules! __openssl_errors_helper {
320+
(
321+
@strings $lib_name:ident($lib_str:expr)
322+
functions { $($func_name:ident($func_str:expr);)* }
323+
reasons { $($reason_name:ident($reason_str:expr);)* }
324+
) => {
325+
static mut STRINGS: [
326+
$crate::export::ERR_STRING_DATA;
327+
2 + $crate::openssl_errors!(@count $($reason_name;)*)
328+
] = [
329+
$crate::export::ERR_STRING_DATA {
330+
error: 0,
331+
string: concat!($lib_str, "\0").as_ptr() as *const $crate::export::c_char,
332+
},
333+
$(
334+
$crate::export::ERR_STRING_DATA {
335+
error: $crate::export::ERR_PACK(0, 0, $lib_name::$reason_name.__as_raw()),
336+
string: concat!($reason_str, "\0").as_ptr() as *const $crate::export::c_char,
337+
},
338+
)*
339+
$crate::export::ERR_STRING_DATA {
340+
error: 0,
341+
string: $crate::export::null(),
342+
}
343+
];
344+
};
345+
(@func_value $n:expr, $func_str:expr) => {
346+
concat!($func_str, "\0").as_ptr() as *const $crate::export::c_char
347+
};
348+
}
349+
} else {
350+
#[doc(hidden)]
351+
#[macro_export]
352+
macro_rules! __openssl_errors_helper {
353+
(
354+
@strings $lib_name:ident($lib_str:expr)
355+
functions { $($func_name:ident($func_str:expr);)* }
356+
reasons { $($reason_name:ident($reason_str:expr);)* }
357+
) => {
358+
static mut STRINGS: [
359+
$crate::export::ERR_STRING_DATA;
360+
2 + $crate::openssl_errors!(@count $($func_name;)* $($reason_name;)*)
361+
] = [
362+
$crate::export::ERR_STRING_DATA {
363+
error: 0,
364+
string: concat!($lib_str, "\0").as_ptr() as *const $crate::export::c_char,
365+
},
366+
$(
367+
$crate::export::ERR_STRING_DATA {
368+
error: $crate::export::ERR_PACK(0, $lib_name::$func_name.__as_raw(), 0),
369+
string: concat!($func_str, "\0").as_ptr() as *const $crate::export::c_char,
370+
},
371+
)*
372+
$(
373+
$crate::export::ERR_STRING_DATA {
374+
error: $crate::export::ERR_PACK(0, 0, $lib_name::$reason_name.__as_raw()),
375+
string: concat!($reason_str, "\0").as_ptr() as *const $crate::export::c_char,
376+
},
377+
)*
378+
$crate::export::ERR_STRING_DATA {
379+
error: 0,
380+
string: $crate::export::null(),
381+
}
382+
];
383+
};
384+
(@func_value $n:expr, $func_str:expr) => {$n};
385+
}
386+
}
387+
}

openssl-errors/tests/test.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cfg_if::cfg_if;
12
use openssl::error::Error;
23

34
openssl_errors::openssl_errors! {
@@ -27,8 +28,15 @@ fn basic() {
2728
error.file().replace(r"\", "/"),
2829
"openssl-errors/tests/test.rs"
2930
);
30-
assert_eq!(error.line(), 19);
31-
assert_eq!(error.data(), None);
31+
assert_eq!(error.line(), 20);
32+
cfg_if! {
33+
if #[cfg(ossl300)] {
34+
// https://github.com/openssl/openssl/issues/12530
35+
assert!(error.data() == None || error.data() == Some(""));
36+
} else {
37+
assert_eq!(error.data(), None);
38+
}
39+
}
3240
}
3341

3442
#[test]
@@ -44,7 +52,7 @@ fn static_data() {
4452
error.file().replace(r"\", "/"),
4553
"openssl-errors/tests/test.rs"
4654
);
47-
assert_eq!(error.line(), 36);
55+
assert_eq!(error.line(), 44);
4856
assert_eq!(error.data(), Some("foobar"));
4957
}
5058

@@ -61,6 +69,6 @@ fn dynamic_data() {
6169
error.file().replace(r"\", "/"),
6270
"openssl-errors/tests/test.rs"
6371
);
64-
assert_eq!(error.line(), 53);
72+
assert_eq!(error.line(), 61);
6573
assert_eq!(error.data(), Some("hello world"));
6674
}

openssl-sys/build/cfgs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub fn get(openssl_version: Option<u64>, libressl_version: Option<u64>) -> Vec<&
3737
} else {
3838
let openssl_version = openssl_version.unwrap();
3939

40+
if openssl_version >= 0x3_00_00_00_0 {
41+
cfgs.push("ossl300");
42+
}
4043
if openssl_version >= 0x1_00_01_00_0 {
4144
cfgs.push("ossl101");
4245
}

openssl-sys/build/expando.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
#define VERSION2(n, v) RUST_VERSION_##n##_##v
55
#define VERSION(n, v) VERSION2(n, v)
66

7+
#define NEW_VERSION2(a, b, c) RUST_VERSION_NEW_OPENSSL_##a##_##b##_##c
8+
#define NEW_VERSION(a, b, c) NEW_VERSION2(a, b, c)
9+
710
#ifdef LIBRESSL_VERSION_NUMBER
811
VERSION(LIBRESSL, LIBRESSL_VERSION_NUMBER)
12+
#elif defined OPENSSL_VERSION_MAJOR
13+
NEW_VERSION(OPENSSL_VERSION_MAJOR, OPENSSL_VERSION_MINOR, OPENSSL_VERSION_PATCH)
914
#else
1015
VERSION(OPENSSL, OPENSSL_VERSION_NUMBER)
1116
#endif
@@ -85,3 +90,7 @@ RUST_CONF_OPENSSL_NO_STDIO
8590
#ifdef OPENSSL_NO_SM3
8691
RUST_CONF_OPENSSL_NO_SM3
8792
#endif
93+
94+
#ifdef OPENSSL_NO_DEPRECATED_3_0
95+
RUST_CONF_OPENSSL_NO_DEPRECATED_3_0
96+
#endif

0 commit comments

Comments
 (0)