Skip to content

Commit 000dada

Browse files
committed
librustls: replace castable macros
They're helpful, but require us to use cbindgen's parse.expand feature which in turn requires nightly, and is broken with nightly since ~April.
1 parent 8985722 commit 000dada

File tree

9 files changed

+284
-272
lines changed

9 files changed

+284
-272
lines changed

librustls/src/acceptor.rs

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,61 @@ use rustls::server::{Accepted, AcceptedAlert, Acceptor};
44
use crate::connection::rustls_connection;
55
use crate::error::{map_error, rustls_io_result, rustls_result};
66
use crate::ffi::{
7-
box_castable, free_box, set_boxed_mut_ptr, to_box, to_boxed_mut_ptr, try_callback,
7+
Castable, OwnershipBox, free_box, set_boxed_mut_ptr, to_box, to_boxed_mut_ptr, try_callback,
88
try_clone_arc, try_mut_from_ptr, try_mut_from_ptr_ptr, try_ref_from_ptr, try_take,
99
};
1010
use crate::io::{CallbackReader, CallbackWriter, rustls_read_callback, rustls_write_callback};
1111
use crate::panic::ffi_panic_boundary;
1212
use crate::rslice::{rustls_slice_bytes, rustls_str};
1313
use crate::server::rustls_server_config;
1414

15-
box_castable! {
16-
/// A buffer and parser for ClientHello bytes.
17-
///
18-
/// This allows reading ClientHello before choosing a rustls_server_config.
19-
///
20-
/// It's useful when the server config will be based on parameters in the
21-
/// ClientHello: server name indication (SNI), ALPN protocols, signature
22-
/// schemes, and cipher suites.
23-
///
24-
/// In particular, if a server wants to do some potentially expensive work
25-
/// to load a certificate for a given hostname, rustls_acceptor allows doing
26-
/// that asynchronously, as opposed to rustls_server_config_builder_set_hello_callback(),
27-
/// which doesn't work well for asynchronous I/O.
28-
///
29-
/// The general flow is:
30-
/// - rustls_acceptor_new()
31-
/// - Loop:
32-
/// - Read bytes from the network it with rustls_acceptor_read_tls().
33-
/// - If successful, parse those bytes with rustls_acceptor_accept().
34-
/// - If that returns RUSTLS_RESULT_ACCEPTOR_NOT_READY, continue.
35-
/// - Otherwise, break.
36-
/// - If rustls_acceptor_accept() returned RUSTLS_RESULT_OK:
37-
/// - Examine the resulting rustls_accepted.
38-
/// - Create or select a rustls_server_config.
39-
/// - Call rustls_accepted_into_connection().
40-
/// - Otherwise, there was a problem with the ClientHello data and the
41-
/// connection should be rejected.
42-
pub struct rustls_acceptor(Acceptor);
15+
/// A buffer and parser for ClientHello bytes.
16+
///
17+
/// This allows reading ClientHello before choosing a rustls_server_config.
18+
///
19+
/// It's useful when the server config will be based on parameters in the
20+
/// ClientHello: server name indication (SNI), ALPN protocols, signature
21+
/// schemes, and cipher suites.
22+
///
23+
/// In particular, if a server wants to do some potentially expensive work
24+
/// to load a certificate for a given hostname, rustls_acceptor allows doing
25+
/// that asynchronously, as opposed to rustls_server_config_builder_set_hello_callback(),
26+
/// which doesn't work well for asynchronous I/O.
27+
///
28+
/// The general flow is:
29+
/// - rustls_acceptor_new()
30+
/// - Loop:
31+
/// - Read bytes from the network it with rustls_acceptor_read_tls().
32+
/// - If successful, parse those bytes with rustls_acceptor_accept().
33+
/// - If that returns RUSTLS_RESULT_ACCEPTOR_NOT_READY, continue.
34+
/// - Otherwise, break.
35+
/// - If rustls_acceptor_accept() returned RUSTLS_RESULT_OK:
36+
/// - Examine the resulting rustls_accepted.
37+
/// - Create or select a rustls_server_config.
38+
/// - Call rustls_accepted_into_connection().
39+
/// - Otherwise, there was a problem with the ClientHello data and the
40+
/// connection should be rejected.
41+
pub struct rustls_acceptor {
42+
_private: [u8; 0],
4343
}
4444

45-
box_castable! {
46-
/// A parsed ClientHello produced by a rustls_acceptor.
47-
///
48-
/// It is used to check server name indication (SNI), ALPN protocols,
49-
/// signature schemes, and cipher suites. It can be combined with a
50-
/// `rustls_server_config` to build a `rustls_connection`.
51-
pub struct rustls_accepted(Option<Accepted>);
45+
impl Castable for rustls_acceptor {
46+
type Ownership = OwnershipBox;
47+
type RustType = Acceptor;
48+
}
49+
50+
/// A parsed ClientHello produced by a rustls_acceptor.
51+
///
52+
/// It is used to check server name indication (SNI), ALPN protocols,
53+
/// signature schemes, and cipher suites. It can be combined with a
54+
/// `rustls_server_config` to build a `rustls_connection`.
55+
pub struct rustls_accepted {
56+
_private: [u8; 0],
57+
}
58+
59+
impl Castable for rustls_accepted {
60+
type Ownership = OwnershipBox;
61+
type RustType = Option<Accepted>;
5262
}
5363

5464
impl rustls_acceptor {
@@ -446,9 +456,14 @@ impl rustls_accepted {
446456
}
447457
}
448458

449-
box_castable! {
450-
/// Represents a TLS alert resulting from accepting a client.
451-
pub struct rustls_accepted_alert(AcceptedAlert);
459+
/// Represents a TLS alert resulting from accepting a client.
460+
pub struct rustls_accepted_alert {
461+
_private: [u8; 0],
462+
}
463+
464+
impl Castable for rustls_accepted_alert {
465+
type Ownership = OwnershipBox;
466+
type RustType = AcceptedAlert;
452467
}
453468

454469
impl rustls_accepted_alert {

librustls/src/certificate.rs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ use rustls::sign::CertifiedKey;
1212
use crate::crypto_provider::{self, rustls_signing_key};
1313
use crate::error::{map_error, rustls_result};
1414
use crate::ffi::{
15-
arc_castable, box_castable, free_arc, free_box, ref_castable, set_arc_mut_ptr,
15+
Castable, OwnershipArc, OwnershipBox, OwnershipRef, free_arc, free_box, set_arc_mut_ptr,
1616
to_arc_const_ptr, to_boxed_mut_ptr, try_box_from_ptr, try_mut_from_ptr, try_ref_from_ptr,
1717
try_ref_from_ptr_ptr, try_slice, try_take,
1818
};
1919
use crate::panic::ffi_panic_boundary;
2020
use crate::rslice::rustls_slice_bytes;
2121

22-
ref_castable! {
23-
/// An X.509 certificate, as used in rustls.
24-
/// Corresponds to `CertificateDer` in the Rust pki-types API.
25-
/// <https://docs.rs/rustls-pki-types/latest/rustls_pki_types/struct.CertificateDer.html>
26-
pub struct rustls_certificate(CertificateDer<'a>);
22+
/// An X.509 certificate, as used in rustls.
23+
/// Corresponds to `CertificateDer` in the Rust pki-types API.
24+
/// <https://docs.rs/rustls-pki-types/latest/rustls_pki_types/struct.CertificateDer.html>
25+
pub struct rustls_certificate<'a> {
26+
_private: [u8; 0],
27+
_marker: PhantomData<&'a ()>,
28+
}
29+
30+
impl<'a> Castable for rustls_certificate<'a> {
31+
type Ownership = OwnershipRef;
32+
type RustType = CertificateDer<'a>;
2733
}
2834

2935
/// Get the DER data of the certificate itself.
@@ -48,13 +54,18 @@ pub extern "C" fn rustls_certificate_get_der(
4854
}
4955
}
5056

51-
arc_castable! {
52-
/// The complete chain of certificates to send during a TLS handshake,
53-
/// plus a private key that matches the end-entity (leaf) certificate.
54-
///
55-
/// Corresponds to `CertifiedKey` in the Rust API.
56-
/// <https://docs.rs/rustls/latest/rustls/sign/struct.CertifiedKey.html>
57-
pub struct rustls_certified_key(CertifiedKey);
57+
/// The complete chain of certificates to send during a TLS handshake,
58+
/// plus a private key that matches the end-entity (leaf) certificate.
59+
///
60+
/// Corresponds to `CertifiedKey` in the Rust API.
61+
/// <https://docs.rs/rustls/latest/rustls/sign/struct.CertifiedKey.html>
62+
pub struct rustls_certified_key {
63+
_private: [u8; 0],
64+
}
65+
66+
impl Castable for rustls_certified_key {
67+
type Ownership = OwnershipArc;
68+
type RustType = CertifiedKey;
5869
}
5970

6071
impl rustls_certified_key {
@@ -270,14 +281,19 @@ impl rustls_certified_key {
270281
}
271282
}
272283

273-
box_castable! {
274-
/// A `rustls_root_cert_store` being constructed.
275-
///
276-
/// A builder can be modified by adding trust anchor root certificates with
277-
/// `rustls_root_cert_store_builder_add_pem`. Once you're done adding root certificates,
278-
/// call `rustls_root_cert_store_builder_build` to turn it into a `rustls_root_cert_store`.
279-
/// This object is not safe for concurrent mutation.
280-
pub struct rustls_root_cert_store_builder(Option<RootCertStoreBuilder>);
284+
/// A `rustls_root_cert_store` being constructed.
285+
///
286+
/// A builder can be modified by adding trust anchor root certificates with
287+
/// `rustls_root_cert_store_builder_add_pem`. Once you're done adding root certificates,
288+
/// call `rustls_root_cert_store_builder_build` to turn it into a `rustls_root_cert_store`.
289+
/// This object is not safe for concurrent mutation.
290+
pub struct rustls_root_cert_store_builder {
291+
_private: [u8; 0],
292+
}
293+
294+
impl Castable for rustls_root_cert_store_builder {
295+
type Ownership = OwnershipBox;
296+
type RustType = Option<RootCertStoreBuilder>;
281297
}
282298

283299
pub(crate) struct RootCertStoreBuilder {
@@ -440,10 +456,15 @@ impl rustls_root_cert_store_builder {
440456
}
441457
}
442458

443-
arc_castable! {
444-
/// A root certificate store.
445-
/// <https://docs.rs/rustls/latest/rustls/struct.RootCertStore.html>
446-
pub struct rustls_root_cert_store(RootCertStore);
459+
/// A root certificate store.
460+
/// <https://docs.rs/rustls/latest/rustls/struct.RootCertStore.html>
461+
pub struct rustls_root_cert_store {
462+
_private: [u8; 0],
463+
}
464+
465+
impl Castable for rustls_root_cert_store {
466+
type Ownership = OwnershipArc;
467+
type RustType = RootCertStore;
447468
}
448469

449470
impl rustls_root_cert_store {

librustls/src/cipher.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
use rustls::SupportedCipherSuite;
22

33
use crate::enums::rustls_tls_version;
4-
use crate::ffi::{ref_castable, try_ref_from_ptr};
4+
use crate::ffi::{Castable, OwnershipRef, try_ref_from_ptr};
55
use crate::panic::ffi_panic_boundary;
66
use crate::rslice::rustls_str;
77

8-
ref_castable! {
9-
/// A cipher suite supported by rustls.
10-
pub struct rustls_supported_ciphersuite(SupportedCipherSuite);
8+
/// A cipher suite supported by rustls.
9+
pub struct rustls_supported_ciphersuite {
10+
_private: [u8; 0],
11+
}
12+
13+
impl Castable for rustls_supported_ciphersuite {
14+
type Ownership = OwnershipRef;
15+
type RustType = SupportedCipherSuite;
1116
}
1217

1318
impl rustls_supported_ciphersuite {

librustls/src/client.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::connection::{Connection, rustls_connection};
1818
use crate::crypto_provider::{self, rustls_crypto_provider, rustls_hpke};
1919
use crate::error::{self, map_error, rustls_result};
2020
use crate::ffi::{
21-
arc_castable, box_castable, free_arc, free_box, set_arc_mut_ptr, set_boxed_mut_ptr,
21+
Castable, OwnershipArc, OwnershipBox, free_arc, free_box, set_arc_mut_ptr, set_boxed_mut_ptr,
2222
to_boxed_mut_ptr, try_box_from_ptr, try_clone_arc, try_mut_from_ptr, try_mut_from_ptr_ptr,
2323
try_ref_from_ptr, try_ref_from_ptr_ptr, try_slice,
2424
};
@@ -29,20 +29,25 @@ use crate::rslice::{rustls_slice_bytes, rustls_slice_slice_bytes, rustls_str};
2929
use crate::userdata::userdata_get;
3030
use crate::verifier::rustls_server_cert_verifier;
3131

32-
box_castable! {
33-
/// A client config being constructed.
34-
///
35-
/// A builder can be modified by, e.g. `rustls_client_config_builder_load_roots_from_file`.
36-
/// Once you're done configuring settings, call `rustls_client_config_builder_build`
37-
/// to turn it into a *rustls_client_config.
38-
///
39-
/// Alternatively, if an error occurs or, you don't wish to build a config,
40-
/// call `rustls_client_config_builder_free` to free the builder directly.
41-
///
42-
/// This object is not safe for concurrent mutation. Under the hood,
43-
/// it corresponds to a `Box<ClientConfig>`.
44-
/// <https://docs.rs/rustls/latest/rustls/struct.ConfigBuilder.html>
45-
pub struct rustls_client_config_builder(ClientConfigBuilder);
32+
/// A client config being constructed.
33+
///
34+
/// A builder can be modified by, e.g. `rustls_client_config_builder_load_roots_from_file`.
35+
/// Once you're done configuring settings, call `rustls_client_config_builder_build`
36+
/// to turn it into a *rustls_client_config.
37+
///
38+
/// Alternatively, if an error occurs or, you don't wish to build a config,
39+
/// call `rustls_client_config_builder_free` to free the builder directly.
40+
///
41+
/// This object is not safe for concurrent mutation. Under the hood,
42+
/// it corresponds to a `Box<ClientConfig>`.
43+
/// <https://docs.rs/rustls/latest/rustls/struct.ConfigBuilder.html>
44+
pub struct rustls_client_config_builder {
45+
_private: [u8; 0],
46+
}
47+
48+
impl Castable for rustls_client_config_builder {
49+
type Ownership = OwnershipBox;
50+
type RustType = ClientConfigBuilder;
4651
}
4752

4853
pub(crate) struct ClientConfigBuilder {
@@ -76,12 +81,17 @@ impl Default for ClientConfigBuilder {
7681
}
7782
}
7883

79-
arc_castable! {
80-
/// A client config that is done being constructed and is now read-only.
81-
///
82-
/// Under the hood, this object corresponds to an `Arc<ClientConfig>`.
83-
/// <https://docs.rs/rustls/latest/rustls/struct.ClientConfig.html>
84-
pub struct rustls_client_config(ClientConfig);
84+
/// A client config that is done being constructed and is now read-only.
85+
///
86+
/// Under the hood, this object corresponds to an `Arc<ClientConfig>`.
87+
/// <https://docs.rs/rustls/latest/rustls/struct.ClientConfig.html>
88+
pub struct rustls_client_config {
89+
_private: [u8; 0],
90+
}
91+
92+
impl Castable for rustls_client_config {
93+
type Ownership = OwnershipArc;
94+
type RustType = ClientConfig;
8595
}
8696

8797
impl rustls_client_config_builder {

librustls/src/connection.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::certificate::rustls_certificate;
1111
use crate::enums::rustls_handshake_kind;
1212
use crate::error::{map_error, rustls_io_result, rustls_result};
1313
use crate::ffi::{
14-
box_castable, free_box, try_callback, try_mut_from_ptr, try_ref_from_ptr, try_slice,
14+
Castable, OwnershipBox, free_box, try_callback, try_mut_from_ptr, try_ref_from_ptr, try_slice,
1515
try_slice_mut,
1616
};
1717
use crate::io::{
@@ -92,9 +92,14 @@ impl std::ops::DerefMut for Connection {
9292
}
9393
}
9494

95-
box_castable! {
96-
/// A C representation of a Rustls `Connection`.
97-
pub struct rustls_connection(Connection);
95+
/// A C representation of a Rustls `Connection`.
96+
pub struct rustls_connection {
97+
_private: [u8; 0],
98+
}
99+
100+
impl Castable for rustls_connection {
101+
type Ownership = OwnershipBox;
102+
type RustType = Connection;
98103
}
99104

100105
impl rustls_connection {

0 commit comments

Comments
 (0)