@@ -5,12 +5,6 @@ use std::io::{self, ErrorKind, Read, Write};
5
5
use std:: net:: { SocketAddr , ToSocketAddrs , TcpStream , TcpListener , Shutdown } ;
6
6
use std:: mem;
7
7
8
- #[ cfg( feature = "openssl" ) ]
9
- pub use self :: openssl:: { Openssl , OpensslClient } ;
10
-
11
- #[ cfg( feature = "security-framework" ) ]
12
- pub use self :: security_framework:: ClientWrapper ;
13
-
14
8
use std:: time:: Duration ;
15
9
16
10
use typeable:: Typeable ;
@@ -413,18 +407,6 @@ impl<F> NetworkConnector for F where F: Fn(&str, u16, &str) -> io::Result<TcpStr
413
407
}
414
408
}
415
409
416
- /// Deprecated
417
- ///
418
- /// Use `SslClient` and `SslServer` instead.
419
- pub trait Ssl {
420
- /// The protected stream.
421
- type Stream : NetworkStream + Send + Clone ;
422
- /// Wrap a client stream with SSL.
423
- fn wrap_client ( & self , stream : HttpStream , host : & str ) -> :: Result < Self :: Stream > ;
424
- /// Wrap a server stream with SSL.
425
- fn wrap_server ( & self , stream : HttpStream ) -> :: Result < Self :: Stream > ;
426
- }
427
-
428
410
/// An abstraction to allow any SSL implementation to be used with client-side HttpsStreams.
429
411
pub trait SslClient < T : NetworkStream + Send + Clone = HttpStream > {
430
412
/// The protected stream.
@@ -441,22 +423,6 @@ pub trait SslServer<T: NetworkStream + Send + Clone = HttpStream> {
441
423
fn wrap_server ( & self , stream : T ) -> :: Result < Self :: Stream > ;
442
424
}
443
425
444
- impl < S : Ssl > SslClient < HttpStream > for S {
445
- type Stream = <S as Ssl >:: Stream ;
446
-
447
- fn wrap_client ( & self , stream : HttpStream , host : & str ) -> :: Result < Self :: Stream > {
448
- Ssl :: wrap_client ( self , stream, host)
449
- }
450
- }
451
-
452
- impl < S : Ssl > SslServer < HttpStream > for S {
453
- type Stream = <S as Ssl >:: Stream ;
454
-
455
- fn wrap_server ( & self , stream : HttpStream ) -> :: Result < Self :: Stream > {
456
- Ssl :: wrap_server ( self , stream)
457
- }
458
- }
459
-
460
426
/// A stream over the HTTP protocol, possibly protected by SSL.
461
427
#[ derive( Debug , Clone ) ]
462
428
pub enum HttpsStream < S : NetworkStream > {
@@ -603,272 +569,9 @@ impl<S: SslClient, C: NetworkConnector<Stream=HttpStream>> NetworkConnector for
603
569
}
604
570
605
571
606
- #[ cfg( all( not( feature = "openssl" ) , not( feature = "security-framework" ) ) ) ]
607
572
#[ doc( hidden) ]
608
573
pub type DefaultConnector = HttpConnector ;
609
574
610
- #[ cfg( feature = "openssl" ) ]
611
- #[ doc( hidden) ]
612
- pub type DefaultConnector = HttpsConnector < self :: openssl:: OpensslClient > ;
613
-
614
- #[ cfg( all( feature = "security-framework" , not( feature = "openssl" ) ) ) ]
615
- pub type DefaultConnector = HttpsConnector < self :: security_framework:: ClientWrapper > ;
616
-
617
- #[ cfg( feature = "openssl" ) ]
618
- mod openssl {
619
- use std:: io;
620
- use std:: net:: { SocketAddr , Shutdown } ;
621
- use std:: path:: Path ;
622
- use std:: sync:: Arc ;
623
- use std:: time:: Duration ;
624
-
625
- use openssl:: ssl:: { Ssl , SslContext , SslStream , SslMethod , SSL_VERIFY_NONE , SSL_VERIFY_PEER , SSL_OP_NO_SSLV2 , SSL_OP_NO_SSLV3 , SSL_OP_NO_COMPRESSION } ;
626
- use openssl:: ssl:: error:: StreamError as SslIoError ;
627
- use openssl:: ssl:: error:: SslError ;
628
- use openssl:: x509:: X509FileType ;
629
- use super :: { NetworkStream , HttpStream } ;
630
-
631
- /// An implementation of `Ssl` for OpenSSL.
632
- ///
633
- /// # Example
634
- ///
635
- /// ```no_run
636
- /// use hyper::Server;
637
- /// use hyper::net::Openssl;
638
- ///
639
- /// let ssl = Openssl::with_cert_and_key("/home/foo/cert", "/home/foo/key").unwrap();
640
- /// Server::https("0.0.0.0:443", ssl).unwrap();
641
- /// ```
642
- ///
643
- /// For complete control, create a `SslContext` with the options you desire
644
- /// and then create `Openssl { context: ctx }
645
- #[ derive( Debug , Clone ) ]
646
- pub struct Openssl {
647
- /// The `SslContext` from openssl crate.
648
- pub context : Arc < SslContext >
649
- }
650
-
651
- /// A client-specific implementation of OpenSSL.
652
- #[ derive( Debug , Clone ) ]
653
- pub struct OpensslClient ( SslContext ) ;
654
-
655
- impl Default for OpensslClient {
656
- fn default ( ) -> OpensslClient {
657
- let mut ctx = SslContext :: new ( SslMethod :: Sslv23 ) . unwrap ( ) ;
658
- ctx. set_default_verify_paths ( ) . unwrap ( ) ;
659
- ctx. set_options ( SSL_OP_NO_SSLV2 | SSL_OP_NO_SSLV3 | SSL_OP_NO_COMPRESSION ) ;
660
- // cipher list taken from curl:
661
- // https://github.com/curl/curl/blob/5bf5f6ebfcede78ef7c2b16daa41c4b7ba266087/lib/vtls/openssl.h#L120
662
- ctx. set_cipher_list ( "ALL!EXPORT!EXPORT40!EXPORT56!aNULL!LOW!RC4@STRENGTH" ) . unwrap ( ) ;
663
- OpensslClient :: new ( ctx)
664
- }
665
- }
666
-
667
- impl OpensslClient {
668
- /// Creates a new OpensslClient with a custom SslContext
669
- pub fn new ( ctx : SslContext ) -> OpensslClient {
670
- OpensslClient ( ctx)
671
- }
672
- }
673
-
674
- impl < T : NetworkStream + Send + Clone > super :: SslClient < T > for OpensslClient {
675
- type Stream = SslStream < T > ;
676
-
677
- #[ cfg( not( windows) ) ]
678
- fn wrap_client ( & self , stream : T , host : & str ) -> :: Result < Self :: Stream > {
679
- let mut ssl = try!( Ssl :: new ( & self . 0 ) ) ;
680
- try!( ssl. set_hostname ( host) ) ;
681
- let host = host. to_owned ( ) ;
682
- ssl. set_verify_callback ( SSL_VERIFY_PEER , move |p, x| :: openssl_verify:: verify_callback ( & host, p, x) ) ;
683
- SslStream :: connect ( ssl, stream) . map_err ( From :: from)
684
- }
685
-
686
-
687
- #[ cfg( windows) ]
688
- fn wrap_client ( & self , stream : T , host : & str ) -> :: Result < Self :: Stream > {
689
- let mut ssl = try!( Ssl :: new ( & self . 0 ) ) ;
690
- try!( ssl. set_hostname ( host) ) ;
691
- SslStream :: connect ( ssl, stream) . map_err ( From :: from)
692
- }
693
- }
694
-
695
- impl Default for Openssl {
696
- fn default ( ) -> Openssl {
697
- Openssl {
698
- context : Arc :: new ( SslContext :: new ( SslMethod :: Sslv23 ) . unwrap_or_else ( |e| {
699
- // if we cannot create a SslContext, that's because of a
700
- // serious problem. just crash.
701
- panic ! ( "{}" , e)
702
- } ) )
703
- }
704
- }
705
- }
706
-
707
- impl Openssl {
708
- /// Ease creating an `Openssl` with a certificate and key.
709
- pub fn with_cert_and_key < C , K > ( cert : C , key : K ) -> Result < Openssl , SslError >
710
- where C : AsRef < Path > , K : AsRef < Path > {
711
- let mut ctx = try!( SslContext :: new ( SslMethod :: Sslv23 ) ) ;
712
- try!( ctx. set_cipher_list ( "DEFAULT" ) ) ;
713
- try!( ctx. set_certificate_chain_file ( cert. as_ref ( ) , X509FileType :: PEM ) ) ;
714
- try!( ctx. set_private_key_file ( key. as_ref ( ) , X509FileType :: PEM ) ) ;
715
- ctx. set_verify ( SSL_VERIFY_NONE , None ) ;
716
- Ok ( Openssl { context : Arc :: new ( ctx) } )
717
- }
718
- }
719
-
720
- impl super :: Ssl for Openssl {
721
- type Stream = SslStream < HttpStream > ;
722
-
723
- fn wrap_client ( & self , stream : HttpStream , host : & str ) -> :: Result < Self :: Stream > {
724
- let ssl = try!( Ssl :: new ( & self . context ) ) ;
725
- try!( ssl. set_hostname ( host) ) ;
726
- SslStream :: connect ( ssl, stream) . map_err ( From :: from)
727
- }
728
-
729
- fn wrap_server ( & self , stream : HttpStream ) -> :: Result < Self :: Stream > {
730
- match SslStream :: accept ( & * self . context , stream) {
731
- Ok ( ssl_stream) => Ok ( ssl_stream) ,
732
- Err ( SslIoError ( e) ) => {
733
- Err ( io:: Error :: new ( io:: ErrorKind :: ConnectionAborted , e) . into ( ) )
734
- } ,
735
- Err ( e) => Err ( e. into ( ) )
736
- }
737
- }
738
- }
739
-
740
- impl < S : NetworkStream > NetworkStream for SslStream < S > {
741
- #[ inline]
742
- fn peer_addr ( & mut self ) -> io:: Result < SocketAddr > {
743
- self . get_mut ( ) . peer_addr ( )
744
- }
745
-
746
- #[ inline]
747
- fn set_read_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
748
- self . get_ref ( ) . set_read_timeout ( dur)
749
- }
750
-
751
- #[ inline]
752
- fn set_write_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
753
- self . get_ref ( ) . set_write_timeout ( dur)
754
- }
755
-
756
- fn close ( & mut self , how : Shutdown ) -> io:: Result < ( ) > {
757
- self . get_mut ( ) . close ( how)
758
- }
759
- }
760
- }
761
-
762
- #[ cfg( feature = "security-framework" ) ]
763
- pub mod security_framework {
764
- use std:: io;
765
- use std:: fmt;
766
- use std:: sync:: { Arc , Mutex } ;
767
- use std:: net:: { Shutdown , SocketAddr } ;
768
- use std:: time:: Duration ;
769
- use security_framework:: secure_transport:: { SslStream , ClientBuilder , ServerBuilder } ;
770
-
771
- use error:: Error ;
772
- use net:: { SslClient , SslServer , HttpStream , NetworkStream } ;
773
-
774
- #[ derive( Default ) ]
775
- pub struct ClientWrapper ( ClientBuilder ) ;
776
-
777
- impl ClientWrapper {
778
- pub fn new ( builder : ClientBuilder ) -> ClientWrapper {
779
- ClientWrapper ( builder)
780
- }
781
- }
782
-
783
- impl SslClient for ClientWrapper {
784
- type Stream = Stream ;
785
-
786
- fn wrap_client ( & self , stream : HttpStream , host : & str ) -> :: Result < Stream > {
787
- match self . 0 . handshake ( host, stream) {
788
- Ok ( s) => Ok ( Stream ( Arc :: new ( Mutex :: new ( s) ) ) ) ,
789
- Err ( e) => Err ( Error :: Ssl ( e. into ( ) ) ) ,
790
- }
791
- }
792
- }
793
-
794
- #[ derive( Clone ) ]
795
- pub struct ServerWrapper ( Arc < ServerBuilder > ) ;
796
-
797
- impl ServerWrapper {
798
- pub fn new ( builder : ServerBuilder ) -> ServerWrapper {
799
- ServerWrapper ( Arc :: new ( builder) )
800
- }
801
- }
802
-
803
- impl SslServer for ServerWrapper {
804
- type Stream = Stream ;
805
-
806
- fn wrap_server ( & self , stream : HttpStream ) -> :: Result < Stream > {
807
- match self . 0 . handshake ( stream) {
808
- Ok ( s) => Ok ( Stream ( Arc :: new ( Mutex :: new ( s) ) ) ) ,
809
- Err ( e) => Err ( Error :: Ssl ( e. into ( ) ) ) ,
810
- }
811
- }
812
- }
813
-
814
- #[ derive( Clone ) ]
815
- pub struct Stream ( Arc < Mutex < SslStream < HttpStream > > > ) ;
816
-
817
- impl io:: Read for Stream {
818
- fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
819
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . read ( buf)
820
- }
821
-
822
- fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
823
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . read_to_end ( buf)
824
- }
825
-
826
- fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
827
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . read_to_string ( buf)
828
- }
829
-
830
- fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < ( ) > {
831
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . read_exact ( buf)
832
- }
833
- }
834
-
835
- impl io:: Write for Stream {
836
- fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
837
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . write ( buf)
838
- }
839
-
840
- fn flush ( & mut self ) -> io:: Result < ( ) > {
841
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . flush ( )
842
- }
843
-
844
- fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
845
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . write_all ( buf)
846
- }
847
-
848
- fn write_fmt ( & mut self , fmt : fmt:: Arguments ) -> io:: Result < ( ) > {
849
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . write_fmt ( fmt)
850
- }
851
- }
852
-
853
- impl NetworkStream for Stream {
854
- fn peer_addr ( & mut self ) -> io:: Result < SocketAddr > {
855
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . get_mut ( ) . peer_addr ( )
856
- }
857
-
858
- fn set_read_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
859
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . get_mut ( ) . set_read_timeout ( dur)
860
- }
861
-
862
- fn set_write_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
863
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . get_mut ( ) . set_write_timeout ( dur)
864
- }
865
-
866
- fn close ( & mut self , how : Shutdown ) -> io:: Result < ( ) > {
867
- self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) ) . get_mut ( ) . close ( how)
868
- }
869
- }
870
- }
871
-
872
575
#[ cfg( test) ]
873
576
mod tests {
874
577
use mock:: MockStream ;
0 commit comments