1#[cfg(feature = "v2_70")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
7use crate::InetAddress;
8use crate::{SocketConnectable, TlsCertificateFlags, ffi};
9#[cfg(feature = "v2_70")]
10#[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
11use glib::signal::{SignalHandlerId, connect_raw};
12use glib::{prelude::*, translate::*};
13#[cfg(feature = "v2_70")]
14#[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
15use std::boxed::Box as Box_;
16
17glib::wrapper! {
18 #[doc(alias = "GTlsCertificate")]
19 pub struct TlsCertificate(Object<ffi::GTlsCertificate, ffi::GTlsCertificateClass>);
20
21 match fn {
22 type_ => || ffi::g_tls_certificate_get_type(),
23 }
24}
25
26impl TlsCertificate {
27 pub const NONE: Option<&'static TlsCertificate> = None;
28
29 #[doc(alias = "g_tls_certificate_new_from_file")]
30 #[doc(alias = "new_from_file")]
31 pub fn from_file(file: impl AsRef<std::path::Path>) -> Result<TlsCertificate, glib::Error> {
32 unsafe {
33 let mut error = std::ptr::null_mut();
34 let ret =
35 ffi::g_tls_certificate_new_from_file(file.as_ref().to_glib_none().0, &mut error);
36 if error.is_null() {
37 Ok(from_glib_full(ret))
38 } else {
39 Err(from_glib_full(error))
40 }
41 }
42 }
43
44 #[cfg(feature = "v2_72")]
45 #[cfg_attr(docsrs, doc(cfg(feature = "v2_72")))]
46 #[doc(alias = "g_tls_certificate_new_from_file_with_password")]
47 #[doc(alias = "new_from_file_with_password")]
48 pub fn from_file_with_password(
49 file: impl AsRef<std::path::Path>,
50 password: &str,
51 ) -> Result<TlsCertificate, glib::Error> {
52 unsafe {
53 let mut error = std::ptr::null_mut();
54 let ret = ffi::g_tls_certificate_new_from_file_with_password(
55 file.as_ref().to_glib_none().0,
56 password.to_glib_none().0,
57 &mut error,
58 );
59 if error.is_null() {
60 Ok(from_glib_full(ret))
61 } else {
62 Err(from_glib_full(error))
63 }
64 }
65 }
66
67 #[doc(alias = "g_tls_certificate_new_from_files")]
68 #[doc(alias = "new_from_files")]
69 pub fn from_files(
70 cert_file: impl AsRef<std::path::Path>,
71 key_file: impl AsRef<std::path::Path>,
72 ) -> Result<TlsCertificate, glib::Error> {
73 unsafe {
74 let mut error = std::ptr::null_mut();
75 let ret = ffi::g_tls_certificate_new_from_files(
76 cert_file.as_ref().to_glib_none().0,
77 key_file.as_ref().to_glib_none().0,
78 &mut error,
79 );
80 if error.is_null() {
81 Ok(from_glib_full(ret))
82 } else {
83 Err(from_glib_full(error))
84 }
85 }
86 }
87
88 #[doc(alias = "g_tls_certificate_new_from_pem")]
89 #[doc(alias = "new_from_pem")]
90 pub fn from_pem(data: &str) -> Result<TlsCertificate, glib::Error> {
91 let length = data.len() as _;
92 unsafe {
93 let mut error = std::ptr::null_mut();
94 let ret =
95 ffi::g_tls_certificate_new_from_pem(data.to_glib_none().0, length, &mut error);
96 if error.is_null() {
97 Ok(from_glib_full(ret))
98 } else {
99 Err(from_glib_full(error))
100 }
101 }
102 }
103
104 #[cfg(feature = "v2_68")]
105 #[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
106 #[doc(alias = "g_tls_certificate_new_from_pkcs11_uris")]
107 #[doc(alias = "new_from_pkcs11_uris")]
108 pub fn from_pkcs11_uris(
109 pkcs11_uri: &str,
110 private_key_pkcs11_uri: Option<&str>,
111 ) -> Result<TlsCertificate, glib::Error> {
112 unsafe {
113 let mut error = std::ptr::null_mut();
114 let ret = ffi::g_tls_certificate_new_from_pkcs11_uris(
115 pkcs11_uri.to_glib_none().0,
116 private_key_pkcs11_uri.to_glib_none().0,
117 &mut error,
118 );
119 if error.is_null() {
120 Ok(from_glib_full(ret))
121 } else {
122 Err(from_glib_full(error))
123 }
124 }
125 }
126
127 #[cfg(feature = "v2_72")]
128 #[cfg_attr(docsrs, doc(cfg(feature = "v2_72")))]
129 #[doc(alias = "g_tls_certificate_new_from_pkcs12")]
130 #[doc(alias = "new_from_pkcs12")]
131 pub fn from_pkcs12(data: &[u8], password: Option<&str>) -> Result<TlsCertificate, glib::Error> {
132 let length = data.len() as _;
133 unsafe {
134 let mut error = std::ptr::null_mut();
135 let ret = ffi::g_tls_certificate_new_from_pkcs12(
136 data.to_glib_none().0,
137 length,
138 password.to_glib_none().0,
139 &mut error,
140 );
141 if error.is_null() {
142 Ok(from_glib_full(ret))
143 } else {
144 Err(from_glib_full(error))
145 }
146 }
147 }
148
149 #[doc(alias = "g_tls_certificate_list_new_from_file")]
150 pub fn list_new_from_file(
151 file: impl AsRef<std::path::Path>,
152 ) -> Result<Vec<TlsCertificate>, glib::Error> {
153 unsafe {
154 let mut error = std::ptr::null_mut();
155 let ret = ffi::g_tls_certificate_list_new_from_file(
156 file.as_ref().to_glib_none().0,
157 &mut error,
158 );
159 if error.is_null() {
160 Ok(FromGlibPtrContainer::from_glib_full(ret))
161 } else {
162 Err(from_glib_full(error))
163 }
164 }
165 }
166}
167
168pub trait TlsCertificateExt: IsA<TlsCertificate> + 'static {
169 #[cfg(feature = "v2_70")]
170 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
171 #[doc(alias = "g_tls_certificate_get_dns_names")]
172 #[doc(alias = "get_dns_names")]
173 #[doc(alias = "dns-names")]
174 fn dns_names(&self) -> Vec<glib::Bytes> {
175 unsafe {
176 FromGlibPtrContainer::from_glib_container(ffi::g_tls_certificate_get_dns_names(
177 self.as_ref().to_glib_none().0,
178 ))
179 }
180 }
181
182 #[cfg(feature = "v2_70")]
183 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
184 #[doc(alias = "g_tls_certificate_get_ip_addresses")]
185 #[doc(alias = "get_ip_addresses")]
186 #[doc(alias = "ip-addresses")]
187 fn ip_addresses(&self) -> Vec<InetAddress> {
188 unsafe {
189 FromGlibPtrContainer::from_glib_container(ffi::g_tls_certificate_get_ip_addresses(
190 self.as_ref().to_glib_none().0,
191 ))
192 }
193 }
194
195 #[doc(alias = "g_tls_certificate_get_issuer")]
196 #[doc(alias = "get_issuer")]
197 #[must_use]
198 fn issuer(&self) -> Option<TlsCertificate> {
199 unsafe {
200 from_glib_none(ffi::g_tls_certificate_get_issuer(
201 self.as_ref().to_glib_none().0,
202 ))
203 }
204 }
205
206 #[cfg(feature = "v2_70")]
207 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
208 #[doc(alias = "g_tls_certificate_get_issuer_name")]
209 #[doc(alias = "get_issuer_name")]
210 #[doc(alias = "issuer-name")]
211 fn issuer_name(&self) -> Option<glib::GString> {
212 unsafe {
213 from_glib_full(ffi::g_tls_certificate_get_issuer_name(
214 self.as_ref().to_glib_none().0,
215 ))
216 }
217 }
218
219 #[cfg(feature = "v2_70")]
220 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
221 #[doc(alias = "g_tls_certificate_get_not_valid_after")]
222 #[doc(alias = "get_not_valid_after")]
223 #[doc(alias = "not-valid-after")]
224 fn not_valid_after(&self) -> Option<glib::DateTime> {
225 unsafe {
226 from_glib_full(ffi::g_tls_certificate_get_not_valid_after(
227 self.as_ref().to_glib_none().0,
228 ))
229 }
230 }
231
232 #[cfg(feature = "v2_70")]
233 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
234 #[doc(alias = "g_tls_certificate_get_not_valid_before")]
235 #[doc(alias = "get_not_valid_before")]
236 #[doc(alias = "not-valid-before")]
237 fn not_valid_before(&self) -> Option<glib::DateTime> {
238 unsafe {
239 from_glib_full(ffi::g_tls_certificate_get_not_valid_before(
240 self.as_ref().to_glib_none().0,
241 ))
242 }
243 }
244
245 #[cfg(feature = "v2_70")]
246 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
247 #[doc(alias = "g_tls_certificate_get_subject_name")]
248 #[doc(alias = "get_subject_name")]
249 #[doc(alias = "subject-name")]
250 fn subject_name(&self) -> Option<glib::GString> {
251 unsafe {
252 from_glib_full(ffi::g_tls_certificate_get_subject_name(
253 self.as_ref().to_glib_none().0,
254 ))
255 }
256 }
257
258 #[doc(alias = "g_tls_certificate_is_same")]
259 fn is_same(&self, cert_two: &impl IsA<TlsCertificate>) -> bool {
260 unsafe {
261 from_glib(ffi::g_tls_certificate_is_same(
262 self.as_ref().to_glib_none().0,
263 cert_two.as_ref().to_glib_none().0,
264 ))
265 }
266 }
267
268 #[doc(alias = "g_tls_certificate_verify")]
269 fn verify(
270 &self,
271 identity: Option<&impl IsA<SocketConnectable>>,
272 trusted_ca: Option<&impl IsA<TlsCertificate>>,
273 ) -> TlsCertificateFlags {
274 unsafe {
275 from_glib(ffi::g_tls_certificate_verify(
276 self.as_ref().to_glib_none().0,
277 identity.map(|p| p.as_ref()).to_glib_none().0,
278 trusted_ca.map(|p| p.as_ref()).to_glib_none().0,
279 ))
280 }
281 }
282
283 fn certificate(&self) -> Option<glib::ByteArray> {
284 ObjectExt::property(self.as_ref(), "certificate")
285 }
286
287 #[doc(alias = "certificate-pem")]
288 fn certificate_pem(&self) -> Option<glib::GString> {
289 ObjectExt::property(self.as_ref(), "certificate-pem")
290 }
291
292 #[cfg(feature = "v2_68")]
293 #[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
294 #[doc(alias = "pkcs11-uri")]
295 fn pkcs11_uri(&self) -> Option<glib::GString> {
296 ObjectExt::property(self.as_ref(), "pkcs11-uri")
297 }
298
299 #[doc(alias = "private-key")]
300 fn private_key(&self) -> Option<glib::ByteArray> {
301 ObjectExt::property(self.as_ref(), "private-key")
302 }
303
304 #[doc(alias = "private-key-pem")]
305 fn private_key_pem(&self) -> Option<glib::GString> {
306 ObjectExt::property(self.as_ref(), "private-key-pem")
307 }
308
309 #[cfg(feature = "v2_68")]
310 #[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
311 #[doc(alias = "private-key-pkcs11-uri")]
312 fn private_key_pkcs11_uri(&self) -> Option<glib::GString> {
313 ObjectExt::property(self.as_ref(), "private-key-pkcs11-uri")
314 }
315
316 #[cfg(feature = "v2_70")]
317 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
318 #[doc(alias = "dns-names")]
319 fn connect_dns_names_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
320 unsafe extern "C" fn notify_dns_names_trampoline<
321 P: IsA<TlsCertificate>,
322 F: Fn(&P) + 'static,
323 >(
324 this: *mut ffi::GTlsCertificate,
325 _param_spec: glib::ffi::gpointer,
326 f: glib::ffi::gpointer,
327 ) {
328 unsafe {
329 let f: &F = &*(f as *const F);
330 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
331 }
332 }
333 unsafe {
334 let f: Box_<F> = Box_::new(f);
335 connect_raw(
336 self.as_ptr() as *mut _,
337 c"notify::dns-names".as_ptr(),
338 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
339 notify_dns_names_trampoline::<Self, F> as *const (),
340 )),
341 Box_::into_raw(f),
342 )
343 }
344 }
345
346 #[cfg(feature = "v2_70")]
347 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
348 #[doc(alias = "ip-addresses")]
349 fn connect_ip_addresses_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
350 unsafe extern "C" fn notify_ip_addresses_trampoline<
351 P: IsA<TlsCertificate>,
352 F: Fn(&P) + 'static,
353 >(
354 this: *mut ffi::GTlsCertificate,
355 _param_spec: glib::ffi::gpointer,
356 f: glib::ffi::gpointer,
357 ) {
358 unsafe {
359 let f: &F = &*(f as *const F);
360 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
361 }
362 }
363 unsafe {
364 let f: Box_<F> = Box_::new(f);
365 connect_raw(
366 self.as_ptr() as *mut _,
367 c"notify::ip-addresses".as_ptr(),
368 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
369 notify_ip_addresses_trampoline::<Self, F> as *const (),
370 )),
371 Box_::into_raw(f),
372 )
373 }
374 }
375
376 #[cfg(feature = "v2_70")]
377 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
378 #[doc(alias = "issuer-name")]
379 fn connect_issuer_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
380 unsafe extern "C" fn notify_issuer_name_trampoline<
381 P: IsA<TlsCertificate>,
382 F: Fn(&P) + 'static,
383 >(
384 this: *mut ffi::GTlsCertificate,
385 _param_spec: glib::ffi::gpointer,
386 f: glib::ffi::gpointer,
387 ) {
388 unsafe {
389 let f: &F = &*(f as *const F);
390 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
391 }
392 }
393 unsafe {
394 let f: Box_<F> = Box_::new(f);
395 connect_raw(
396 self.as_ptr() as *mut _,
397 c"notify::issuer-name".as_ptr(),
398 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
399 notify_issuer_name_trampoline::<Self, F> as *const (),
400 )),
401 Box_::into_raw(f),
402 )
403 }
404 }
405
406 #[cfg(feature = "v2_70")]
407 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
408 #[doc(alias = "not-valid-after")]
409 fn connect_not_valid_after_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
410 unsafe extern "C" fn notify_not_valid_after_trampoline<
411 P: IsA<TlsCertificate>,
412 F: Fn(&P) + 'static,
413 >(
414 this: *mut ffi::GTlsCertificate,
415 _param_spec: glib::ffi::gpointer,
416 f: glib::ffi::gpointer,
417 ) {
418 unsafe {
419 let f: &F = &*(f as *const F);
420 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
421 }
422 }
423 unsafe {
424 let f: Box_<F> = Box_::new(f);
425 connect_raw(
426 self.as_ptr() as *mut _,
427 c"notify::not-valid-after".as_ptr(),
428 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
429 notify_not_valid_after_trampoline::<Self, F> as *const (),
430 )),
431 Box_::into_raw(f),
432 )
433 }
434 }
435
436 #[cfg(feature = "v2_70")]
437 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
438 #[doc(alias = "not-valid-before")]
439 fn connect_not_valid_before_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
440 unsafe extern "C" fn notify_not_valid_before_trampoline<
441 P: IsA<TlsCertificate>,
442 F: Fn(&P) + 'static,
443 >(
444 this: *mut ffi::GTlsCertificate,
445 _param_spec: glib::ffi::gpointer,
446 f: glib::ffi::gpointer,
447 ) {
448 unsafe {
449 let f: &F = &*(f as *const F);
450 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
451 }
452 }
453 unsafe {
454 let f: Box_<F> = Box_::new(f);
455 connect_raw(
456 self.as_ptr() as *mut _,
457 c"notify::not-valid-before".as_ptr(),
458 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
459 notify_not_valid_before_trampoline::<Self, F> as *const (),
460 )),
461 Box_::into_raw(f),
462 )
463 }
464 }
465
466 #[cfg(feature = "v2_70")]
467 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
468 #[doc(alias = "subject-name")]
469 fn connect_subject_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
470 unsafe extern "C" fn notify_subject_name_trampoline<
471 P: IsA<TlsCertificate>,
472 F: Fn(&P) + 'static,
473 >(
474 this: *mut ffi::GTlsCertificate,
475 _param_spec: glib::ffi::gpointer,
476 f: glib::ffi::gpointer,
477 ) {
478 unsafe {
479 let f: &F = &*(f as *const F);
480 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
481 }
482 }
483 unsafe {
484 let f: Box_<F> = Box_::new(f);
485 connect_raw(
486 self.as_ptr() as *mut _,
487 c"notify::subject-name".as_ptr(),
488 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
489 notify_subject_name_trampoline::<Self, F> as *const (),
490 )),
491 Box_::into_raw(f),
492 )
493 }
494 }
495}
496
497impl<O: IsA<TlsCertificate>> TlsCertificateExt for O {}