1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(any(feature = "v2_66", feature = "dox"))]
use std::ptr;

use glib::prelude::*;
#[cfg(any(feature = "v2_60", feature = "dox"))]
use glib::{translate::*, IntoStrV};

#[cfg(any(feature = "v2_66", feature = "dox"))]
use crate::TlsChannelBindingType;
use crate::TlsConnection;

pub trait TlsConnectionExtManual {
    /// Query the TLS backend for TLS channel binding data of `type_` for `self`.
    ///
    /// This call retrieves TLS channel binding data as specified in RFC
    /// [5056](https://tools.ietf.org/html/rfc5056), RFC
    /// [5929](https://tools.ietf.org/html/rfc5929), and related RFCs. The
    /// binding data is returned in `data`. The `data` is resized by the callee
    /// using [`glib::ByteArray`][crate::glib::ByteArray] buffer management and will be freed when the `data`
    /// is destroyed by `g_byte_array_unref()`. If `data` is [`None`], it will only
    /// check whether TLS backend is able to fetch the data (e.g. whether `type_`
    /// is supported by the TLS backend). It does not guarantee that the data
    /// will be available though. That could happen if TLS connection does not
    /// support `type_` or the binding data is not available yet due to additional
    /// negotiation or input required.
    /// ## `type_`
    /// [`TlsChannelBindingType`][crate::TlsChannelBindingType] type of data to fetch
    ///
    /// # Returns
    ///
    /// [`true`] on success, [`false`] otherwise
    ///
    /// ## `data`
    /// [`glib::ByteArray`][crate::glib::ByteArray] is
    ///  filled with the binding data, or [`None`]
    #[cfg(any(feature = "v2_66", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_66")))]
    #[doc(alias = "g_tls_connection_get_channel_binding_data")]
    #[doc(alias = "get_channel_binding_data")]
    fn channel_binding_data(
        &self,
        type_: TlsChannelBindingType,
    ) -> Result<glib::ByteArray, glib::Error>;

    /// Sets the list of application-layer protocols to advertise that the
    /// caller is willing to speak on this connection. The
    /// Application-Layer Protocol Negotiation (ALPN) extension will be
    /// used to negotiate a compatible protocol with the peer; use
    /// [`TlsConnectionExt::negotiated_protocol()`][crate::prelude::TlsConnectionExt::negotiated_protocol()] to find the negotiated
    /// protocol after the handshake. Specifying [`None`] for the the value
    /// of `protocols` will disable ALPN negotiation.
    ///
    /// See [IANA TLS ALPN Protocol IDs](https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml`alpn`-protocol-ids)
    /// for a list of registered protocol IDs.
    /// ## `protocols`
    /// a [`None`]-terminated
    ///  array of ALPN protocol names (eg, "http/1.1", "h2"), or [`None`]
    #[cfg(any(feature = "v2_60", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_60")))]
    #[doc(alias = "g_tls_connection_set_advertised_protocols")]
    fn set_advertised_protocols(&self, protocols: impl IntoStrV);
}

impl<O: IsA<TlsConnection>> TlsConnectionExtManual for O {
    #[cfg(any(feature = "v2_66", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_66")))]
    fn channel_binding_data(
        &self,
        type_: TlsChannelBindingType,
    ) -> Result<glib::ByteArray, glib::Error> {
        unsafe {
            let data = ptr::null_mut();
            let mut error = ptr::null_mut();
            let _ = ffi::g_tls_connection_get_channel_binding_data(
                self.as_ptr() as *mut _,
                type_.into_glib(),
                data,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_none(data))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[cfg(any(feature = "v2_60", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2_60")))]
    fn set_advertised_protocols(&self, protocols: impl IntoStrV) {
        unsafe {
            protocols.run_with_strv(|protocols| {
                ffi::g_tls_connection_set_advertised_protocols(
                    self.as_ref().to_glib_none().0,
                    protocols.as_ptr() as *mut _,
                );
            })
        }
    }
}