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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::SocketConnectable;
use crate::TlsCertificateFlags;
use glib::object::IsA;
use glib::translate::*;
use glib::StaticType;
use std::fmt;
use std::ptr;

glib::wrapper! {
    /// A certificate used for TLS authentication and encryption.
    /// This can represent either a certificate only (eg, the certificate
    /// received by a client from a server), or the combination of
    /// a certificate and a private key (which is needed when acting as a
    /// [`TlsServerConnection`][crate::TlsServerConnection]).
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`TlsCertificateExt`][trait@crate::prelude::TlsCertificateExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GTlsCertificate")]
    pub struct TlsCertificate(Object<ffi::GTlsCertificate, ffi::GTlsCertificateClass>);

    match fn {
        type_ => || ffi::g_tls_certificate_get_type(),
    }
}

impl TlsCertificate {
    /// Creates a [`TlsCertificate`][crate::TlsCertificate] from the PEM-encoded data in `file`. The
    /// returned certificate will be the first certificate found in `file`. As
    /// of GLib 2.44, if `file` contains more certificates it will try to load
    /// a certificate chain. All certificates will be verified in the order
    /// found (top-level certificate should be the last one in the file) and
    /// the `property::TlsCertificate::issuer` property of each certificate will be set
    /// accordingly if the verification succeeds. If any certificate in the
    /// chain cannot be verified, the first certificate in the file will
    /// still be returned.
    ///
    /// If `file` cannot be read or parsed, the function will return [`None`] and
    /// set `error`. Otherwise, this behaves like
    /// [`from_pem()`][Self::from_pem()].
    /// ## `file`
    /// file containing a PEM-encoded certificate to import
    ///
    /// # Returns
    ///
    /// the new certificate, or [`None`] on error
    #[doc(alias = "g_tls_certificate_new_from_file")]
    #[doc(alias = "new_from_file")]
    pub fn from_file<P: AsRef<std::path::Path>>(file: P) -> Result<TlsCertificate, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret =
                ffi::g_tls_certificate_new_from_file(file.as_ref().to_glib_none().0, &mut error);
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Creates a [`TlsCertificate`][crate::TlsCertificate] from the PEM-encoded data in `cert_file`
    /// and `key_file`. The returned certificate will be the first certificate
    /// found in `cert_file`. As of GLib 2.44, if `cert_file` contains more
    /// certificates it will try to load a certificate chain. All
    /// certificates will be verified in the order found (top-level
    /// certificate should be the last one in the file) and the
    /// `property::TlsCertificate::issuer` property of each certificate will be set
    /// accordingly if the verification succeeds. If any certificate in the
    /// chain cannot be verified, the first certificate in the file will
    /// still be returned.
    ///
    /// If either file cannot be read or parsed, the function will return
    /// [`None`] and set `error`. Otherwise, this behaves like
    /// [`from_pem()`][Self::from_pem()].
    /// ## `cert_file`
    /// file containing one or more PEM-encoded
    ///  certificates to import
    /// ## `key_file`
    /// file containing a PEM-encoded private key
    ///  to import
    ///
    /// # Returns
    ///
    /// the new certificate, or [`None`] on error
    #[doc(alias = "g_tls_certificate_new_from_files")]
    #[doc(alias = "new_from_files")]
    pub fn from_files<P: AsRef<std::path::Path>, Q: AsRef<std::path::Path>>(
        cert_file: P,
        key_file: Q,
    ) -> Result<TlsCertificate, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_tls_certificate_new_from_files(
                cert_file.as_ref().to_glib_none().0,
                key_file.as_ref().to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Creates a [`TlsCertificate`][crate::TlsCertificate] from the PEM-encoded data in `data`. If
    /// `data` includes both a certificate and a private key, then the
    /// returned certificate will include the private key data as well. (See
    /// the `property::TlsCertificate::private-key-pem` property for information about
    /// supported formats.)
    ///
    /// The returned certificate will be the first certificate found in
    /// `data`. As of GLib 2.44, if `data` contains more certificates it will
    /// try to load a certificate chain. All certificates will be verified in
    /// the order found (top-level certificate should be the last one in the
    /// file) and the `property::TlsCertificate::issuer` property of each certificate
    /// will be set accordingly if the verification succeeds. If any
    /// certificate in the chain cannot be verified, the first certificate in
    /// the file will still be returned.
    /// ## `data`
    /// PEM-encoded certificate data
    /// ## `length`
    /// the length of `data`, or -1 if it's 0-terminated.
    ///
    /// # Returns
    ///
    /// the new certificate, or [`None`] if `data` is invalid
    #[doc(alias = "g_tls_certificate_new_from_pem")]
    #[doc(alias = "new_from_pem")]
    pub fn from_pem(data: &str) -> Result<TlsCertificate, glib::Error> {
        let length = data.len() as isize;
        unsafe {
            let mut error = ptr::null_mut();
            let ret =
                ffi::g_tls_certificate_new_from_pem(data.to_glib_none().0, length, &mut error);
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Creates one or more `GTlsCertificates` from the PEM-encoded
    /// data in `file`. If `file` cannot be read or parsed, the function will
    /// return [`None`] and set `error`. If `file` does not contain any
    /// PEM-encoded certificates, this will return an empty list and not
    /// set `error`.
    /// ## `file`
    /// file containing PEM-encoded certificates to import
    ///
    /// # Returns
    ///
    /// a
    /// `GList` containing [`TlsCertificate`][crate::TlsCertificate] objects. You must free the list
    /// and its contents when you are done with it.
    #[doc(alias = "g_tls_certificate_list_new_from_file")]
    pub fn list_new_from_file<P: AsRef<std::path::Path>>(
        file: P,
    ) -> Result<Vec<TlsCertificate>, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_tls_certificate_list_new_from_file(
                file.as_ref().to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(FromGlibPtrContainer::from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }
}

pub const NONE_TLS_CERTIFICATE: Option<&TlsCertificate> = None;

/// Trait containing all [`struct@TlsCertificate`] methods.
///
/// # Implementors
///
/// [`TlsCertificate`][struct@crate::TlsCertificate]
pub trait TlsCertificateExt: 'static {
    /// Gets the [`TlsCertificate`][crate::TlsCertificate] representing `self`'s issuer, if known
    ///
    /// # Returns
    ///
    /// The certificate of `self`'s issuer,
    /// or [`None`] if `self` is self-signed or signed with an unknown
    /// certificate.
    #[doc(alias = "g_tls_certificate_get_issuer")]
    #[doc(alias = "get_issuer")]
    fn issuer(&self) -> Option<TlsCertificate>;

    /// Check if two [`TlsCertificate`][crate::TlsCertificate] objects represent the same certificate.
    /// The raw DER byte data of the two certificates are checked for equality.
    /// This has the effect that two certificates may compare equal even if
    /// their `property::TlsCertificate::issuer`, `property::TlsCertificate::private-key`, or
    /// `property::TlsCertificate::private-key-pem` properties differ.
    /// ## `cert_two`
    /// second certificate to compare
    ///
    /// # Returns
    ///
    /// whether the same or not
    #[doc(alias = "g_tls_certificate_is_same")]
    fn is_same<P: IsA<TlsCertificate>>(&self, cert_two: &P) -> bool;

    /// This verifies `self` and returns a set of [`TlsCertificateFlags`][crate::TlsCertificateFlags]
    /// indicating any problems found with it. This can be used to verify a
    /// certificate outside the context of making a connection, or to
    /// check a certificate against a CA that is not part of the system
    /// CA database.
    ///
    /// If `identity` is not [`None`], `self`'s name(s) will be compared against
    /// it, and [`TlsCertificateFlags::BAD_IDENTITY`][crate::TlsCertificateFlags::BAD_IDENTITY] will be set in the return
    /// value if it does not match. If `identity` is [`None`], that bit will
    /// never be set in the return value.
    ///
    /// If `trusted_ca` is not [`None`], then `self` (or one of the certificates
    /// in its chain) must be signed by it, or else
    /// [`TlsCertificateFlags::UNKNOWN_CA`][crate::TlsCertificateFlags::UNKNOWN_CA] will be set in the return value. If
    /// `trusted_ca` is [`None`], that bit will never be set in the return
    /// value.
    ///
    /// (All other [`TlsCertificateFlags`][crate::TlsCertificateFlags] values will always be set or unset
    /// as appropriate.)
    /// ## `identity`
    /// the expected peer identity
    /// ## `trusted_ca`
    /// the certificate of a trusted authority
    ///
    /// # Returns
    ///
    /// the appropriate [`TlsCertificateFlags`][crate::TlsCertificateFlags]
    #[doc(alias = "g_tls_certificate_verify")]
    fn verify<P: IsA<SocketConnectable>, Q: IsA<TlsCertificate>>(
        &self,
        identity: Option<&P>,
        trusted_ca: Option<&Q>,
    ) -> TlsCertificateFlags;

    /// The DER (binary) encoded representation of the certificate.
    /// This property and the `property::TlsCertificate::certificate-pem` property
    /// represent the same data, just in different forms.
    fn certificate(&self) -> Option<glib::ByteArray>;

    /// The PEM (ASCII) encoded representation of the certificate.
    /// This property and the `property::TlsCertificate::certificate`
    /// property represent the same data, just in different forms.
    #[doc(alias = "certificate-pem")]
    fn certificate_pem(&self) -> Option<glib::GString>;
}

impl<O: IsA<TlsCertificate>> TlsCertificateExt for O {
    fn issuer(&self) -> Option<TlsCertificate> {
        unsafe {
            from_glib_none(ffi::g_tls_certificate_get_issuer(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn is_same<P: IsA<TlsCertificate>>(&self, cert_two: &P) -> bool {
        unsafe {
            from_glib(ffi::g_tls_certificate_is_same(
                self.as_ref().to_glib_none().0,
                cert_two.as_ref().to_glib_none().0,
            ))
        }
    }

    fn verify<P: IsA<SocketConnectable>, Q: IsA<TlsCertificate>>(
        &self,
        identity: Option<&P>,
        trusted_ca: Option<&Q>,
    ) -> TlsCertificateFlags {
        unsafe {
            from_glib(ffi::g_tls_certificate_verify(
                self.as_ref().to_glib_none().0,
                identity.map(|p| p.as_ref()).to_glib_none().0,
                trusted_ca.map(|p| p.as_ref()).to_glib_none().0,
            ))
        }
    }

    fn certificate(&self) -> Option<glib::ByteArray> {
        unsafe {
            let mut value = glib::Value::from_type(<glib::ByteArray as StaticType>::static_type());
            glib::gobject_ffi::g_object_get_property(
                self.to_glib_none().0 as *mut glib::gobject_ffi::GObject,
                b"certificate\0".as_ptr() as *const _,
                value.to_glib_none_mut().0,
            );
            value
                .get()
                .expect("Return Value for property `certificate` getter")
        }
    }

    fn certificate_pem(&self) -> Option<glib::GString> {
        unsafe {
            let mut value = glib::Value::from_type(<glib::GString as StaticType>::static_type());
            glib::gobject_ffi::g_object_get_property(
                self.to_glib_none().0 as *mut glib::gobject_ffi::GObject,
                b"certificate-pem\0".as_ptr() as *const _,
                value.to_glib_none_mut().0,
            );
            value
                .get()
                .expect("Return Value for property `certificate-pem` getter")
        }
    }
}

impl fmt::Display for TlsCertificate {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("TlsCertificate")
    }
}