1
2
3use crate::prelude::*;
4
5
6
7
8#[ derive (Clone) ]
9#[ cfg (feature = "hss-config") ]
10pub struct Configuration {
11 pub endpoint : Endpoint,
12 #[ cfg (feature = "hss-handler") ]
13 pub handler : Option<HandlerDynArc>,
14 #[ cfg (feature = "hss-server-mt" ) ]
15 pub threads : Option<usize>,
16 #[ cfg (feature = "hss-server-profiling" ) ]
17 pub profiling : Option<path::PathBuf>,
18}
19
20
21#[ derive (Clone) ]
22#[ cfg (feature = "hss-config") ]
23pub struct Endpoint {
24 pub address : EndpointAddress,
25 pub protocol : EndpointProtocol,
26 pub security : EndpointSecurity,
27}
28
29
30#[ derive (Clone) ]
31#[ allow (variant_size_differences) ]
32#[ cfg (feature = "hss-config") ]
33pub enum EndpointAddress {
34 Socket (net::SocketAddr),
35 #[ cfg (unix) ]
36 Descriptor (u32),
37}
38
39
40#[ derive (Clone) ]
41#[ cfg (feature = "hss-config") ]
42pub enum EndpointProtocol {
43 #[ cfg (feature = "hyper--server-http1") ]
44 Http1,
45 #[ cfg (feature = "hyper--server-http2") ]
46 Http2,
47 #[ cfg (feature = "hyper--server-http1") ]
48 #[ cfg (feature = "hyper--server-http2") ]
49 Http12,
50 Generic,
51}
52
53
54#[ derive (Clone) ]
55#[ cfg (feature = "hss-config") ]
56pub enum EndpointSecurity {
57 Insecure,
58 #[ cfg (feature = "hss-tls-rust") ]
59 RustTls (RustTlsCertificate),
60 #[ cfg (feature = "hss-tls-native") ]
61 NativeTls (NativeTlsCertificate),
62}
63
64
65#[ derive (Clone) ]
66#[ cfg (feature = "hss-tls-rust") ]
67pub struct RustTlsCertificate {
68 pub certified : rustls::sign::CertifiedKey,
69}
70
71#[ derive (Clone) ]
72#[ cfg (feature = "hss-tls-native") ]
73pub struct NativeTlsCertificate {
74 pub identity : natls::Identity,
75}
76
77
78
79
80#[ cfg (feature = "hss-config") ]
81impl Configuration {
82
83 pub fn builder () -> ConfigurationBuilder {
84 ConfigurationBuilder::new ()
85 }
86
87 pub fn localhost () -> ConfigurationBuilder {
88 #[ cfg (feature = "hss-tls-any") ]
89 return Self::localhost_https ();
90 #[ cfg (not (feature = "hss-tls-any")) ]
91 return Self::localhost_http ();
92 }
93
94 pub fn localhost_http () -> ConfigurationBuilder {
95 Configuration::builder ()
96 .with_endpoint (Endpoint::localhost_http ())
97 }
98
99 #[ cfg (feature = "hss-tls-any") ]
100 pub fn localhost_https () -> ConfigurationBuilder {
101 Configuration::builder ()
102 .with_endpoint (Endpoint::localhost_https ())
103 }
104}
105
106
107
108
109#[ cfg (feature = "hss-config") ]
110impl Default for Endpoint {
111
112 fn default () -> Self {
113 Endpoint {
114 address : EndpointAddress::default (),
115 protocol : EndpointProtocol::default (),
116 security : EndpointSecurity::default (),
117 }
118 }
119}
120
121
122#[ cfg (feature = "hss-config") ]
123impl Default for EndpointAddress {
124
125 fn default () -> Self {
126 EndpointAddress::Socket (net::SocketAddr::from (([127,0,0,1], 0)))
127 }
128}
129
130
131#[ cfg (feature = "hss-config") ]
132impl Default for EndpointProtocol {
133
134 #[ cfg (feature = "hyper--server-http") ]
135 fn default () -> Self {
136 Self::with_http_support (true, true)
137 }
138
139 #[ cfg (not (feature = "hyper--server-http")) ]
140 fn default () -> Self {
141 EndpointProtocol::Generic
142 }
143}
144
145
146#[ cfg (feature = "hss-config") ]
147impl Default for EndpointSecurity {
148
149 fn default () -> Self {
150 EndpointSecurity::Insecure
151 }
152}
153
154
155
156
157#[ cfg (feature = "hss-config") ]
158impl Endpoint {
159
160 pub fn localhost_http () -> Self {
161
162 let mut _endpoint = Endpoint {
163 .. Default::default ()
164 };
165
166 _endpoint.address = EndpointAddress::localhost_http ();
167
168 _endpoint
169 }
170
171 #[ cfg (feature = "hss-tls-any") ]
172 pub fn localhost_https () -> Self {
173
174 let _security = EndpointSecurity::Insecure;
175 #[ cfg (feature = "hss-tls-rust") ]
176 let _security = {
177 let _certificate = RustTlsCertificate::localhost () .or_panic (0xf64b30c4);
178 EndpointSecurity::RustTls (_certificate)
179 };
180 #[ cfg (feature = "hss-tls-native") ]
181 let _security = {
182 let _certificate = NativeTlsCertificate::localhost () .or_panic (0xf6a595a9);
183 EndpointSecurity::NativeTls (_certificate)
184 };
185
186 let mut _endpoint = Endpoint {
187 .. Default::default ()
188 };
189
190 _endpoint.address = EndpointAddress::localhost_https ();
191 _endpoint.security = _security;
192
193 _endpoint
194 }
195}
196
197
198#[ cfg (feature = "hss-config") ]
199impl EndpointAddress {
200
201 pub fn localhost_http () -> Self {
202 Self::from_socket_address (([127,0,0,1], 8080))
203 }
204
205 pub fn localhost_https () -> Self {
206 Self::from_socket_address (([127,0,0,1], 8443))
207 }
208
209 pub fn from_socket_address (_address : impl Into<net::SocketAddr>) -> Self {
210 EndpointAddress::Socket (_address.into ())
211 }
212
213 pub fn from_socket_address_parse (_address : &(impl net::ToSocketAddrs + ?Sized)) -> ServerResult<Self> {
214 let mut _addresses = _address.to_socket_addrs () ?;
215 let _address = if let Some (_address) = _addresses.next () {
216 _address
217 } else {
218 return Err (error_with_message (0x3a20b501, "no socket addresses resolved"));
219 };
220 if _addresses.next () .is_some () {
221 return Err (error_with_message (0x93c154c9, "multiple socket addresses resolved"));
222 }
223 Ok (Self::from_socket_address (_address))
224 }
225
226 #[ cfg (unix) ]
227 pub fn from_descriptor (_descriptor : u32) -> Self {
228 EndpointAddress::Descriptor (_descriptor)
229 }
230}
231
232
233#[ cfg (feature = "hss-config") ]
234impl EndpointProtocol {
235
236 pub fn supports_http1 (&self) -> bool {
237 match self {
238 #[ cfg (feature = "hyper--server-http1") ]
239 EndpointProtocol::Http1 => true,
240 #[ cfg (feature = "hyper--server-http2") ]
241 EndpointProtocol::Http2 => false,
242 #[ cfg (feature = "hyper--server-http1") ]
243 #[ cfg (feature = "hyper--server-http2") ]
244 EndpointProtocol::Http12 => true,
245 EndpointProtocol::Generic => false,
246 }
247 }
248
249 pub fn supports_http2 (&self) -> bool {
250 match self {
251 #[ cfg (feature = "hyper--server-http1") ]
252 EndpointProtocol::Http1 => false,
253 #[ cfg (feature = "hyper--server-http2") ]
254 EndpointProtocol::Http2 => true,
255 #[ cfg (feature = "hyper--server-http1") ]
256 #[ cfg (feature = "hyper--server-http2") ]
257 EndpointProtocol::Http12 => true,
258 EndpointProtocol::Generic => false,
259 }
260 }
261
262 pub fn supports_http1_only (&self) -> bool {
263 self.supports_http1 () && ! self.supports_http2 ()
264 }
265
266 pub fn supports_http2_only (&self) -> bool {
267 self.supports_http2 () && ! self.supports_http1 ()
268 }
269
270 #[ cfg (feature = "hyper--server-http") ]
271 pub fn with_http_support (_http1 : bool, _http2 : bool) -> Self {
272
273 #[ cfg (feature = "hyper--server-http1") ]
274 #[ cfg (feature = "hyper--server-http2") ]
275 if _http1 && _http2 {
276 return EndpointProtocol::Http12;
277 }
278
279 #[ cfg (feature = "hyper--server-http1") ]
280 if _http1 {
281 return EndpointProtocol::Http1;
282 }
283
284 #[ cfg (feature = "hyper--server-http2") ]
285 if _http2 {
286 return EndpointProtocol::Http2;
287 }
288
289 return EndpointProtocol::Generic;
290 }
291}
292
293
294
295
296#[ cfg (feature = "hss-config") ]
297impl Endpoint {
298
299 pub fn url (&self) -> String {
300 let _scheme = if self.security.supports_tls () { "https" } else { "http" };
301 format! ("{}://{}/", _scheme, self.address.url_authority ())
302 }
303}
304
305
306#[ cfg (feature = "hss-config") ]
307impl EndpointAddress {
308
309 pub fn url_authority (&self) -> String {
310 match self {
311 EndpointAddress::Socket (_address) =>
312 _address.to_string (),
313 #[ cfg (unix) ]
314 EndpointAddress::Descriptor (_descriptor) =>
315 format! ("[descriptor:{}]", _descriptor),
316 }
317 }
318}
319
320
321#[ cfg (feature = "hss-config") ]
322impl EndpointSecurity {
323
324 pub fn supports_tls (&self) -> bool {
325 match self {
326 EndpointSecurity::Insecure => false,
327 #[ cfg (feature = "hss-tls-rust") ]
328 EndpointSecurity::RustTls (_) => true,
329 #[ cfg (feature = "hss-tls-native") ]
330 EndpointSecurity::NativeTls (_) => true,
331 }
332 }
333}
334
335
336
337
338#[ cfg (feature = "hss-config") ]
339pub struct ConfigurationBuilder {
340 endpoint : Option<Endpoint>,
341 #[ cfg (feature = "hss-handler") ]
342 handler : Option<HandlerDynArc>,
343 #[ cfg (feature = "hss-routes") ]
344 routes : Option<RoutesBuilder>,
345 #[ cfg (feature = "hss-server-mt") ]
346 threads : Option<usize>,
347 #[ cfg (feature = "hss-server-profiling" ) ]
348 profiling : Option<path::PathBuf>,
349}
350
351
352#[ cfg (feature = "hss-config") ]
353impl ConfigurationBuilder {
354
355 pub fn new () -> Self {
356 Self {
357 endpoint : None,
358 #[ cfg (feature = "hss-handler") ]
359 handler : None,
360 #[ cfg (feature = "hss-routes") ]
361 routes : None,
362 #[ cfg (feature = "hss-server-mt") ]
363 threads : None,
364 #[ cfg (feature = "hss-server-profiling" ) ]
365 profiling : None,
366 }
367 }
368
369 pub fn build (self) -> ServerResult<Configuration> {
370
371 let ConfigurationBuilder {
372 endpoint : _endpoint,
373 #[ cfg (feature = "hss-handler") ]
374 handler : _handler,
375 #[ cfg (feature = "hss-routes") ]
376 routes : _routes,
377 #[ cfg (feature = "hss-server-mt") ]
378 threads : _threads,
379 #[ cfg (feature = "hss-server-profiling" ) ]
380 profiling : _profiling,
381 } = self;
382
383 let _endpoint = if let Some (_endpoint) = _endpoint {
384 _endpoint
385 } else {
386 Endpoint::default ()
387 };
388
389 #[ cfg (feature = "hss-handler") ]
390 #[ cfg (feature = "hss-routes") ]
391 if _handler.is_some () && _routes.is_some () {
392 return Err (error_with_message (0xc7d24cd3, "both handler and routes specified"))
393 }
394
395 #[ cfg (feature = "hss-handler") ]
396 let mut _handler_0 = None;
397 #[ cfg (feature = "hss-handler") ]
398 if _handler_0.is_none () {
399 if let Some (_handler) = _handler {
400 _handler_0 = Some (_handler);
401 }
402 }
403 #[ cfg (feature = "hss-routes") ]
404 if _handler_0.is_none () {
405 if let Some (_routes) = _routes {
406 let _routes = _routes.build () ?;
407 _handler_0 = Some (HandlerDynArc::new (_routes));
408 }
409 }
410
411 let _configuration = Configuration {
412 endpoint : _endpoint,
413 #[ cfg (feature = "hss-handler") ]
414 handler : _handler_0,
415 #[ cfg (feature = "hss-server-mt") ]
416 threads : _threads,
417 #[ cfg (feature = "hss-server-profiling" ) ]
418 profiling : _profiling,
419 };
420
421 Ok (_configuration)
422 }
423}
424
425
426#[ cfg (feature = "hss-config") ]
427impl ConfigurationBuilder {
428
429 pub fn with_endpoint (mut self, _endpoint : Endpoint) -> Self {
430 self.endpoint = Some (_endpoint);
431 self
432 }
433
434 pub fn with_endpoint_address (mut self, _address : EndpointAddress) -> Self {
435 self.endpoint_mut () .address = _address;
436 self
437 }
438
439 pub fn with_endpoint_socket_address (self, _address : impl Into<net::SocketAddr>) -> Self {
440 let _address = EndpointAddress::from_socket_address (_address);
441 self.with_endpoint_address (_address)
442 }
443
444 pub fn with_endpoint_socket_address_parse (self, _address : &(impl net::ToSocketAddrs + ?Sized)) -> ServerResult<Self> {
445 let _address = EndpointAddress::from_socket_address_parse (_address) ?;
446 Ok (self.with_endpoint_address (_address))
447 }
448
449 #[ cfg (unix) ]
450 pub fn with_endpoint_descriptor (self, _descriptor : u32) -> Self {
451 let _address = EndpointAddress::from_descriptor (_descriptor);
452 self.with_endpoint_address (_address)
453 }
454
455 pub fn with_endpoint_protocol (mut self, _protocol : EndpointProtocol) -> Self {
456 self.endpoint_mut () .protocol = _protocol;
457 self
458 }
459
460 pub fn with_endpoint_security (mut self, _security : EndpointSecurity) -> Self {
461 self.endpoint_mut () .security = _security;
462 self
463 }
464
465 fn endpoint_mut (&mut self) -> &mut Endpoint {
466 self.endpoint.get_or_insert_with (Endpoint::default)
467 }
468}
469
470
471#[ cfg (feature = "hss-config") ]
472#[ cfg (feature = "hss-tls-rust") ]
473impl ConfigurationBuilder {
474
475 pub fn with_endpoint_certificate_rustls (mut self, _certificate : RustTlsCertificate) -> Self {
476 self.endpoint_mut () .security = EndpointSecurity::RustTls (_certificate);
477 self
478 }
479
480 pub fn with_endpoint_certificate_rustls_from_pem_file (self, _path : impl AsRef<path::Path>) -> ServerResult<Self> {
481 let _certificate = RustTlsCertificate::load_from_pem_file (_path) ?;
482 Ok (self.with_endpoint_certificate_rustls (_certificate))
483 }
484
485 pub fn with_endpoint_certificate_rustls_from_pem_data (self, _data : impl AsRef<[u8]>) -> ServerResult<Self> {
486 let _certificate = RustTlsCertificate::load_from_pem_data (_data) ?;
487 Ok (self.with_endpoint_certificate_rustls (_certificate))
488 }
489}
490
491
492#[ cfg (feature = "hss-config") ]
493#[ cfg (feature = "hss-tls-native") ]
494impl ConfigurationBuilder {
495
496 pub fn with_endpoint_certificate_native (mut self, _certificate : NativeTlsCertificate) -> Self {
497 self.endpoint_mut () .security = EndpointSecurity::NativeTls (_certificate);
498 self
499 }
500
501 pub fn with_endpoint_certificate_native_from_pkcs12_file (self, _path : impl AsRef<path::Path>, _password : &str) -> ServerResult<Self> {
502 let _certificate = NativeTlsCertificate::load_from_pkcs12_file (_path, _password) ?;
503 Ok (self.with_endpoint_certificate_native (_certificate))
504 }
505
506 pub fn with_endpoint_certificate_native_from_pkcs12_data (self, _data : impl AsRef<[u8]>, _password : &str) -> ServerResult<Self> {
507 let _certificate = NativeTlsCertificate::load_from_pkcs12_data (_data, _password) ?;
508 Ok (self.with_endpoint_certificate_native (_certificate))
509 }
510}
511
512
513#[ cfg (feature = "hss-config") ]
514#[ cfg (feature = "hss-handler") ]
515impl ConfigurationBuilder {
516
517 pub fn with_handler <H, F, RB> (self, _handler : H) -> Self
518 where
519 H : Handler<Future = F, ResponseBody = RB, ResponseBodyError = RB::Error> + Send + Sync + 'static,
520 F : Future<Output = ServerResult<Response<RB>>> + Send + 'static,
521 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
522 RB::Error : Error + Send + Sync + 'static,
523 {
524 let _handler : H = _handler.into ();
525 self.with_handler_dyn (_handler)
526 }
527
528 pub fn with_handler_fn_sync <I, C, RB> (self, _handler : I) -> Self
529 where
530 I : Into<HandlerFnSync<C, RB>>,
531 C : Fn (Request<Body>) -> ServerResult<Response<RB>> + Send + Sync + 'static,
532 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
533 RB::Error : Error + Send + Sync + 'static,
534 {
535 let _handler : HandlerFnSync<C, RB> = _handler.into ();
536 self.with_handler_dyn (_handler)
537 }
538
539 pub fn with_handler_fn_async <I, C, F, RB> (self, _handler : I) -> Self
540 where
541 I : Into<HandlerFnAsync<C, F, RB>>,
542 C : Fn (Request<Body>) -> F + Send + Sync + 'static,
543 F : Future<Output = ServerResult<Response<RB>>> + Send + 'static,
544 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
545 RB::Error : Error + Send + Sync + 'static,
546 {
547 let _handler : HandlerFnAsync<C, F, RB> = _handler.into ();
548 self.with_handler_dyn (_handler)
549 }
550
551 pub fn with_handler_dyn <H> (self, _handler : H) -> Self
552 where
553 H : HandlerDyn,
554 {
555 let _handler : H = _handler.into ();
556 let _handler = HandlerDynArc::new (_handler);
557 self.with_handler_arc (_handler)
558 }
559
560 pub fn with_handler_arc <I> (mut self, _handler : I) -> Self
561 where
562 I : Into<HandlerDynArc>,
563 {
564 self.handler = Some (_handler.into ());
565 self
566 }
567}
568
569
570#[ cfg (feature = "hss-config") ]
571#[ cfg (feature = "hss-routes") ]
572impl ConfigurationBuilder {
573
574 #[ allow (single_use_lifetimes) ]
575 pub fn with_route <'a, P, H, F, RB> (self, _paths : P, _handler : H) -> Self
576 where
577 P : Into<RoutePaths<'a>>,
578 H : Handler<Future = F, ResponseBody = RB, ResponseBodyError = RB::Error> + Send + Sync + 'static,
579 F : Future<Output = ServerResult<Response<RB>>> + Send + 'static,
580 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
581 RB::Error : Error + Send + Sync + 'static,
582 {
583 let _handler : H = _handler.into ();
584 self.with_route_dyn (_paths, _handler)
585 }
586
587 #[ allow (single_use_lifetimes) ]
588 pub fn with_route_fn_sync <'a, P, I, C, RB> (self, _paths : P, _handler : I) -> Self
589 where
590 P : Into<RoutePaths<'a>>,
591 I : Into<HandlerFnSync<C, RB>>,
592 C : Fn (Request<Body>) -> ServerResult<Response<RB>> + Send + Sync + 'static,
593 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
594 RB::Error : Error + Send + Sync + 'static,
595 {
596 let _handler : HandlerFnSync<C, RB> = _handler.into ();
597 self.with_route_dyn (_paths, _handler)
598 }
599
600 #[ allow (single_use_lifetimes) ]
601 pub fn with_route_fn_async <'a, P, I, C, F, RB> (self, _paths : P, _handler : I) -> Self
602 where
603 P : Into<RoutePaths<'a>>,
604 I : Into<HandlerFnAsync<C, F, RB>>,
605 C : Fn (Request<Body>) -> F + Send + Sync + 'static,
606 F : Future<Output = ServerResult<Response<RB>>> + Send + 'static,
607 RB : BodyTrait<Data = Bytes> + Send + Sync + 'static,
608 RB::Error : Error + Send + Sync + 'static,
609 {
610 let _handler : HandlerFnAsync<C, F, RB> = _handler.into ();
611 self.with_route_dyn (_paths, _handler)
612 }
613
614 #[ allow (single_use_lifetimes) ]
615 pub fn with_route_dyn <'a, P, H> (self, _paths : P, _handler : H) -> Self
616 where
617 H : HandlerDyn,
618 P : Into<RoutePaths<'a>>,
619 {
620 let _handler : H = _handler.into ();
621 let _handler = HandlerDynArc::new (_handler);
622 self.with_route_arc (_paths, _handler)
623 }
624
625 #[ allow (single_use_lifetimes) ]
626 pub fn with_route_arc <'a, P, I> (mut self, _paths : P, _handler : I) -> Self
627 where
628 I : Into<HandlerDynArc>,
629 P : Into<RoutePaths<'a>>,
630 {
631 let _routes = self.routes.take () .unwrap_or_else (RoutesBuilder::new);
632 let _routes = _routes.with_route_arc (_paths, _handler);
633 self.routes = Some (_routes);
634 self
635 }
636
637 pub fn with_routes (mut self, _routes : impl Into<Routes>) -> Self {
638 let _routes = _routes.into ();
639 self.routes = Some (_routes.into_builder ());
640 self
641 }
642}
643
644
645
646
647#[ cfg (feature = "hss-tls-rust") ]
648impl RustTlsCertificate {
649
650 pub fn load_from_pem_file (_path : impl AsRef<path::Path>) -> ServerResult<Self> {
651 let _data = fs::read (_path) ?;
652 Self::load_from_pem_data (&_data)
653 }
654
655 pub fn load_from_pem_data (_data : impl AsRef<[u8]>) -> ServerResult<Self> {
656
657 let _data = _data.as_ref ();
658
659 let _certificates = {
660 let mut _data = _data;
661 rustls_pem::certs (&mut _data) .or_wrap (0x1004be65) ?
662 };
663 let _private_keys = {
664 let mut _data = _data;
665 rustls_pem::pkcs8_private_keys (&mut _data) .or_wrap (0x57b13036) ?
666 };
667
668 Self::load_from_parts (
669 _certificates.iter () .map (|_part| _part.as_slice ()),
670 _private_keys.iter () .map (|_part| _part.as_slice ()),
671 )
672 }
673
674 pub fn load_from_parts <'a> (mut _certificates : impl Iterator<Item = &'a [u8]>, mut _private_keys : impl Iterator<Item = &'a [u8]>) -> ServerResult<Self> {
675 let _certificates = {
676 let _certificates : Vec<_> = _certificates.map (<[u8]>::to_vec) .map (rustls::Certificate) .collect ();
677 if _certificates.is_empty () {
678 return Err (error_with_message (0xc6991697, "no certificates found"));
679 }
680 _certificates
681 };
682 let _private_key = {
683 if let Some (_private_key) = _private_keys.next () {
684 if _private_keys.next () .is_some () {
685 return Err (error_with_message (0xa5a124ef, "multiple private keys found"));
686 }
687 rustls::PrivateKey (_private_key.to_vec ())
688 } else {
689 return Err (error_with_message (0x84af61dd, "no private key found"));
690 }
691 };
692 Self::load_from_parts_0 (_certificates, _private_key)
693 }
694
695 fn load_from_parts_0 (_certificates : Vec<rustls::Certificate>, _private_key : rustls::PrivateKey) -> ServerResult<Self> {
696 let _certified = {
697 let _private_key = rustls::sign::any_supported_type (&_private_key) .map_err (|_| error_with_message (0x5c4797d0, "invalid private key")) ?;
698 rustls::sign::CertifiedKey::new (_certificates, Arc::new (_private_key))
699 };
700 let _certificate = RustTlsCertificate {
701 certified : _certified,
702 };
703 Ok (_certificate)
704 }
705
706 pub fn localhost () -> ServerResult<Self> {
707 let _bundle = include_str! ("./tls-testing-bundle.pem");
708 Self::load_from_pem_data (_bundle)
709 }
710}
711
712
713
714
715#[ cfg (feature = "hss-tls-native") ]
716impl NativeTlsCertificate {
717
718 pub fn load_from_pkcs12_file (_path : impl AsRef<path::Path>, _password : &str) -> ServerResult<Self> {
719 let _data = fs::read (_path) ?;
720 Self::load_from_pkcs12_data (&_data, _password)
721 }
722
723 pub fn load_from_pkcs12_data (_data : impl AsRef<[u8]>, _password : &str) -> ServerResult<Self> {
724
725 let _data = _data.as_ref ();
726
727 let _identity = natls::Identity::from_pkcs12 (_data, _password) .or_wrap (0x93817715) ?;
728
729 let _certificate = NativeTlsCertificate {
730 identity : _identity,
731 };
732
733 Ok (_certificate)
734 }
735
736 pub fn localhost () -> ServerResult<Self> {
737 let _bundle = include_bytes! ("./tls-testing-bundle.p12");
738 Self::load_from_pkcs12_data (_bundle, "bundle")
739 }
740}
741