1#[cfg(feature = "v2_70")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
7use crate::InetAddress;
8use crate::{ffi, SocketConnectable, TlsCertificateFlags};
9#[cfg(feature = "v2_70")]
10#[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
11use glib::signal::{connect_raw, SignalHandlerId};
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 let f: &F = &*(f as *const F);
329 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
330 }
331 unsafe {
332 let f: Box_<F> = Box_::new(f);
333 connect_raw(
334 self.as_ptr() as *mut _,
335 c"notify::dns-names".as_ptr() as *const _,
336 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
337 notify_dns_names_trampoline::<Self, F> as *const (),
338 )),
339 Box_::into_raw(f),
340 )
341 }
342 }
343
344 #[cfg(feature = "v2_70")]
345 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
346 #[doc(alias = "ip-addresses")]
347 fn connect_ip_addresses_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
348 unsafe extern "C" fn notify_ip_addresses_trampoline<
349 P: IsA<TlsCertificate>,
350 F: Fn(&P) + 'static,
351 >(
352 this: *mut ffi::GTlsCertificate,
353 _param_spec: glib::ffi::gpointer,
354 f: glib::ffi::gpointer,
355 ) {
356 let f: &F = &*(f as *const F);
357 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
358 }
359 unsafe {
360 let f: Box_<F> = Box_::new(f);
361 connect_raw(
362 self.as_ptr() as *mut _,
363 c"notify::ip-addresses".as_ptr() as *const _,
364 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
365 notify_ip_addresses_trampoline::<Self, F> as *const (),
366 )),
367 Box_::into_raw(f),
368 )
369 }
370 }
371
372 #[cfg(feature = "v2_70")]
373 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
374 #[doc(alias = "issuer-name")]
375 fn connect_issuer_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
376 unsafe extern "C" fn notify_issuer_name_trampoline<
377 P: IsA<TlsCertificate>,
378 F: Fn(&P) + 'static,
379 >(
380 this: *mut ffi::GTlsCertificate,
381 _param_spec: glib::ffi::gpointer,
382 f: glib::ffi::gpointer,
383 ) {
384 let f: &F = &*(f as *const F);
385 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
386 }
387 unsafe {
388 let f: Box_<F> = Box_::new(f);
389 connect_raw(
390 self.as_ptr() as *mut _,
391 c"notify::issuer-name".as_ptr() as *const _,
392 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
393 notify_issuer_name_trampoline::<Self, F> as *const (),
394 )),
395 Box_::into_raw(f),
396 )
397 }
398 }
399
400 #[cfg(feature = "v2_70")]
401 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
402 #[doc(alias = "not-valid-after")]
403 fn connect_not_valid_after_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
404 unsafe extern "C" fn notify_not_valid_after_trampoline<
405 P: IsA<TlsCertificate>,
406 F: Fn(&P) + 'static,
407 >(
408 this: *mut ffi::GTlsCertificate,
409 _param_spec: glib::ffi::gpointer,
410 f: glib::ffi::gpointer,
411 ) {
412 let f: &F = &*(f as *const F);
413 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
414 }
415 unsafe {
416 let f: Box_<F> = Box_::new(f);
417 connect_raw(
418 self.as_ptr() as *mut _,
419 c"notify::not-valid-after".as_ptr() as *const _,
420 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
421 notify_not_valid_after_trampoline::<Self, F> as *const (),
422 )),
423 Box_::into_raw(f),
424 )
425 }
426 }
427
428 #[cfg(feature = "v2_70")]
429 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
430 #[doc(alias = "not-valid-before")]
431 fn connect_not_valid_before_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
432 unsafe extern "C" fn notify_not_valid_before_trampoline<
433 P: IsA<TlsCertificate>,
434 F: Fn(&P) + 'static,
435 >(
436 this: *mut ffi::GTlsCertificate,
437 _param_spec: glib::ffi::gpointer,
438 f: glib::ffi::gpointer,
439 ) {
440 let f: &F = &*(f as *const F);
441 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
442 }
443 unsafe {
444 let f: Box_<F> = Box_::new(f);
445 connect_raw(
446 self.as_ptr() as *mut _,
447 c"notify::not-valid-before".as_ptr() as *const _,
448 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
449 notify_not_valid_before_trampoline::<Self, F> as *const (),
450 )),
451 Box_::into_raw(f),
452 )
453 }
454 }
455
456 #[cfg(feature = "v2_70")]
457 #[cfg_attr(docsrs, doc(cfg(feature = "v2_70")))]
458 #[doc(alias = "subject-name")]
459 fn connect_subject_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
460 unsafe extern "C" fn notify_subject_name_trampoline<
461 P: IsA<TlsCertificate>,
462 F: Fn(&P) + 'static,
463 >(
464 this: *mut ffi::GTlsCertificate,
465 _param_spec: glib::ffi::gpointer,
466 f: glib::ffi::gpointer,
467 ) {
468 let f: &F = &*(f as *const F);
469 f(TlsCertificate::from_glib_borrow(this).unsafe_cast_ref())
470 }
471 unsafe {
472 let f: Box_<F> = Box_::new(f);
473 connect_raw(
474 self.as_ptr() as *mut _,
475 c"notify::subject-name".as_ptr() as *const _,
476 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
477 notify_subject_name_trampoline::<Self, F> as *const (),
478 )),
479 Box_::into_raw(f),
480 )
481 }
482 }
483}
484
485impl<O: IsA<TlsCertificate>> TlsCertificateExt for O {}