gio/
tls_connection.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(feature = "v2_66")]
4use std::ptr;
5
6use glib::prelude::*;
7#[cfg(feature = "v2_60")]
8use glib::translate::*;
9
10#[cfg(feature = "v2_66")]
11use crate::TlsChannelBindingType;
12use crate::TlsConnection;
13
14mod sealed {
15    pub trait Sealed {}
16    impl<T: super::IsA<super::TlsConnection>> Sealed for T {}
17}
18
19pub trait TlsConnectionExtManual: sealed::Sealed + IsA<TlsConnection> {
20    /// Query the TLS backend for TLS channel binding data of @type_ for @self.
21    ///
22    /// This call retrieves TLS channel binding data as specified in RFC
23    /// [5056](https://tools.ietf.org/html/rfc5056), RFC
24    /// [5929](https://tools.ietf.org/html/rfc5929), and related RFCs.  The
25    /// binding data is returned in @data.  The @data is resized by the callee
26    /// using #GByteArray buffer management and will be freed when the @data
27    /// is destroyed by g_byte_array_unref(). If @data is [`None`], it will only
28    /// check whether TLS backend is able to fetch the data (e.g. whether @type_
29    /// is supported by the TLS backend). It does not guarantee that the data
30    /// will be available though.  That could happen if TLS connection does not
31    /// support @type_ or the binding data is not available yet due to additional
32    /// negotiation or input required.
33    /// ## `type_`
34    /// #GTlsChannelBindingType type of data to fetch
35    ///
36    /// # Returns
37    ///
38    /// [`true`] on success, [`false`] otherwise
39    ///
40    /// ## `data`
41    /// #GByteArray is
42    ///        filled with the binding data, or [`None`]
43    #[cfg(feature = "v2_66")]
44    #[cfg_attr(docsrs, doc(cfg(feature = "v2_66")))]
45    #[doc(alias = "g_tls_connection_get_channel_binding_data")]
46    #[doc(alias = "get_channel_binding_data")]
47    fn channel_binding_data(
48        &self,
49        type_: TlsChannelBindingType,
50    ) -> Result<glib::ByteArray, glib::Error> {
51        unsafe {
52            let data = ptr::null_mut();
53            let mut error = ptr::null_mut();
54            let _ = crate::ffi::g_tls_connection_get_channel_binding_data(
55                self.as_ptr() as *mut _,
56                type_.into_glib(),
57                data,
58                &mut error,
59            );
60            if error.is_null() {
61                Ok(from_glib_none(data))
62            } else {
63                Err(from_glib_full(error))
64            }
65        }
66    }
67
68    /// Sets the list of application-layer protocols to advertise that the
69    /// caller is willing to speak on this connection. The
70    /// Application-Layer Protocol Negotiation (ALPN) extension will be
71    /// used to negotiate a compatible protocol with the peer; use
72    /// g_tls_connection_get_negotiated_protocol() to find the negotiated
73    /// protocol after the handshake.  Specifying [`None`] for the the value
74    /// of @protocols will disable ALPN negotiation.
75    ///
76    /// See [IANA TLS ALPN Protocol IDs](https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids)
77    /// for a list of registered protocol IDs.
78    /// ## `protocols`
79    /// a [`None`]-terminated
80    ///   array of ALPN protocol names (eg, "http/1.1", "h2"), or [`None`]
81    #[cfg(feature = "v2_60")]
82    #[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
83    #[doc(alias = "g_tls_connection_set_advertised_protocols")]
84    fn set_advertised_protocols(&self, protocols: impl IntoStrV) {
85        unsafe {
86            protocols.run_with_strv(|protocols| {
87                crate::ffi::g_tls_connection_set_advertised_protocols(
88                    self.as_ref().to_glib_none().0,
89                    protocols.as_ptr() as *mut _,
90                );
91            })
92        }
93    }
94}
95
96impl<O: IsA<TlsConnection>> TlsConnectionExtManual for O {}