@@ -681,205 +681,6 @@ def test_malformed_key(self):
681681 """Wrapping with a badly formatted key (syntax error)"""
682682 self .bad_cert_test ("badkey.pem" )
683683
684- @ignore_deprecation
685- def test_match_hostname (self ):
686- def ok (cert , hostname ):
687- ssl .match_hostname (cert , hostname )
688- def fail (cert , hostname ):
689- self .assertRaises (ssl .CertificateError ,
690- ssl .match_hostname , cert , hostname )
691-
692- # -- Hostname matching --
693-
694- cert = {'subject' : ((('commonName' , 'example.com' ),),)}
695- ok (cert , 'example.com' )
696- ok (cert , 'ExAmple.cOm' )
697- fail (cert , 'www.example.com' )
698- fail (cert , '.example.com' )
699- fail (cert , 'example.org' )
700- fail (cert , 'exampleXcom' )
701-
702- cert = {'subject' : ((('commonName' , '*.a.com' ),),)}
703- ok (cert , 'foo.a.com' )
704- fail (cert , 'bar.foo.a.com' )
705- fail (cert , 'a.com' )
706- fail (cert , 'Xa.com' )
707- fail (cert , '.a.com' )
708-
709- # only match wildcards when they are the only thing
710- # in left-most segment
711- cert = {'subject' : ((('commonName' , 'f*.com' ),),)}
712- fail (cert , 'foo.com' )
713- fail (cert , 'f.com' )
714- fail (cert , 'bar.com' )
715- fail (cert , 'foo.a.com' )
716- fail (cert , 'bar.foo.com' )
717-
718- # NULL bytes are bad, CVE-2013-4073
719- cert = {'subject' : ((('commonName' ,
720- 'null.python.org\x00 example.org' ),),)}
721- ok (cert , 'null.python.org\x00 example.org' ) # or raise an error?
722- fail (cert , 'example.org' )
723- fail (cert , 'null.python.org' )
724-
725- # error cases with wildcards
726- cert = {'subject' : ((('commonName' , '*.*.a.com' ),),)}
727- fail (cert , 'bar.foo.a.com' )
728- fail (cert , 'a.com' )
729- fail (cert , 'Xa.com' )
730- fail (cert , '.a.com' )
731-
732- cert = {'subject' : ((('commonName' , 'a.*.com' ),),)}
733- fail (cert , 'a.foo.com' )
734- fail (cert , 'a..com' )
735- fail (cert , 'a.com' )
736-
737- # wildcard doesn't match IDNA prefix 'xn--'
738- idna = 'püthon.python.org' .encode ("idna" ).decode ("ascii" )
739- cert = {'subject' : ((('commonName' , idna ),),)}
740- ok (cert , idna )
741- cert = {'subject' : ((('commonName' , 'x*.python.org' ),),)}
742- fail (cert , idna )
743- cert = {'subject' : ((('commonName' , 'xn--p*.python.org' ),),)}
744- fail (cert , idna )
745-
746- # wildcard in first fragment and IDNA A-labels in sequent fragments
747- # are supported.
748- idna = 'www*.pythön.org' .encode ("idna" ).decode ("ascii" )
749- cert = {'subject' : ((('commonName' , idna ),),)}
750- fail (cert , 'www.pythön.org' .encode ("idna" ).decode ("ascii" ))
751- fail (cert , 'www1.pythön.org' .encode ("idna" ).decode ("ascii" ))
752- fail (cert , 'ftp.pythön.org' .encode ("idna" ).decode ("ascii" ))
753- fail (cert , 'pythön.org' .encode ("idna" ).decode ("ascii" ))
754-
755- # Slightly fake real-world example
756- cert = {'notAfter' : 'Jun 26 21:41:46 2011 GMT' ,
757- 'subject' : ((('commonName' , 'linuxfrz.org' ),),),
758- 'subjectAltName' : (('DNS' , 'linuxfr.org' ),
759- ('DNS' , 'linuxfr.com' ),
760- ('othername' , '<unsupported>' ))}
761- ok (cert , 'linuxfr.org' )
762- ok (cert , 'linuxfr.com' )
763- # Not a "DNS" entry
764- fail (cert , '<unsupported>' )
765- # When there is a subjectAltName, commonName isn't used
766- fail (cert , 'linuxfrz.org' )
767-
768- # A pristine real-world example
769- cert = {'notAfter' : 'Dec 18 23:59:59 2011 GMT' ,
770- 'subject' : ((('countryName' , 'US' ),),
771- (('stateOrProvinceName' , 'California' ),),
772- (('localityName' , 'Mountain View' ),),
773- (('organizationName' , 'Google Inc' ),),
774- (('commonName' , 'mail.google.com' ),))}
775- ok (cert , 'mail.google.com' )
776- fail (cert , 'gmail.com' )
777- # Only commonName is considered
778- fail (cert , 'California' )
779-
780- # -- IPv4 matching --
781- cert = {'subject' : ((('commonName' , 'example.com' ),),),
782- 'subjectAltName' : (('DNS' , 'example.com' ),
783- ('IP Address' , '10.11.12.13' ),
784- ('IP Address' , '14.15.16.17' ),
785- ('IP Address' , '127.0.0.1' ))}
786- ok (cert , '10.11.12.13' )
787- ok (cert , '14.15.16.17' )
788- # socket.inet_ntoa(socket.inet_aton('127.1')) == '127.0.0.1'
789- fail (cert , '127.1' )
790- fail (cert , '14.15.16.17 ' )
791- fail (cert , '14.15.16.17 extra data' )
792- fail (cert , '14.15.16.18' )
793- fail (cert , 'example.net' )
794-
795- # -- IPv6 matching --
796- if socket_helper .IPV6_ENABLED :
797- cert = {'subject' : ((('commonName' , 'example.com' ),),),
798- 'subjectAltName' : (
799- ('DNS' , 'example.com' ),
800- ('IP Address' , '2001:0:0:0:0:0:0:CAFE\n ' ),
801- ('IP Address' , '2003:0:0:0:0:0:0:BABA\n ' ))}
802- ok (cert , '2001::cafe' )
803- ok (cert , '2003::baba' )
804- fail (cert , '2003::baba ' )
805- fail (cert , '2003::baba extra data' )
806- fail (cert , '2003::bebe' )
807- fail (cert , 'example.net' )
808-
809- # -- Miscellaneous --
810-
811- # Neither commonName nor subjectAltName
812- cert = {'notAfter' : 'Dec 18 23:59:59 2011 GMT' ,
813- 'subject' : ((('countryName' , 'US' ),),
814- (('stateOrProvinceName' , 'California' ),),
815- (('localityName' , 'Mountain View' ),),
816- (('organizationName' , 'Google Inc' ),))}
817- fail (cert , 'mail.google.com' )
818-
819- # No DNS entry in subjectAltName but a commonName
820- cert = {'notAfter' : 'Dec 18 23:59:59 2099 GMT' ,
821- 'subject' : ((('countryName' , 'US' ),),
822- (('stateOrProvinceName' , 'California' ),),
823- (('localityName' , 'Mountain View' ),),
824- (('commonName' , 'mail.google.com' ),)),
825- 'subjectAltName' : (('othername' , 'blabla' ), )}
826- ok (cert , 'mail.google.com' )
827-
828- # No DNS entry subjectAltName and no commonName
829- cert = {'notAfter' : 'Dec 18 23:59:59 2099 GMT' ,
830- 'subject' : ((('countryName' , 'US' ),),
831- (('stateOrProvinceName' , 'California' ),),
832- (('localityName' , 'Mountain View' ),),
833- (('organizationName' , 'Google Inc' ),)),
834- 'subjectAltName' : (('othername' , 'blabla' ),)}
835- fail (cert , 'google.com' )
836-
837- # Empty cert / no cert
838- self .assertRaises (ValueError , ssl .match_hostname , None , 'example.com' )
839- self .assertRaises (ValueError , ssl .match_hostname , {}, 'example.com' )
840-
841- # Issue #17980: avoid denials of service by refusing more than one
842- # wildcard per fragment.
843- cert = {'subject' : ((('commonName' , 'a*b.example.com' ),),)}
844- with self .assertRaisesRegex (
845- ssl .CertificateError ,
846- "partial wildcards in leftmost label are not supported" ):
847- ssl .match_hostname (cert , 'axxb.example.com' )
848-
849- cert = {'subject' : ((('commonName' , 'www.*.example.com' ),),)}
850- with self .assertRaisesRegex (
851- ssl .CertificateError ,
852- "wildcard can only be present in the leftmost label" ):
853- ssl .match_hostname (cert , 'www.sub.example.com' )
854-
855- cert = {'subject' : ((('commonName' , 'a*b*.example.com' ),),)}
856- with self .assertRaisesRegex (
857- ssl .CertificateError ,
858- "too many wildcards" ):
859- ssl .match_hostname (cert , 'axxbxxc.example.com' )
860-
861- cert = {'subject' : ((('commonName' , '*' ),),)}
862- with self .assertRaisesRegex (
863- ssl .CertificateError ,
864- "sole wildcard without additional labels are not support" ):
865- ssl .match_hostname (cert , 'host' )
866-
867- cert = {'subject' : ((('commonName' , '*.com' ),),)}
868- with self .assertRaisesRegex (
869- ssl .CertificateError ,
870- r"hostname 'com' doesn't match '\*.com'" ):
871- ssl .match_hostname (cert , 'com' )
872-
873- # extra checks for _inet_paton()
874- for invalid in ['1' , '' , '1.2.3' , '256.0.0.1' , '127.0.0.1/24' ]:
875- with self .assertRaises (ValueError ):
876- ssl ._inet_paton (invalid )
877- for ipaddr in ['127.0.0.1' , '192.168.0.1' ]:
878- self .assertTrue (ssl ._inet_paton (ipaddr ))
879- if socket_helper .IPV6_ENABLED :
880- for ipaddr in ['::1' , '2001:db8:85a3::8a2e:370:7334' ]:
881- self .assertTrue (ssl ._inet_paton (ipaddr ))
882-
883684 def test_server_side (self ):
884685 # server_hostname doesn't work for server sockets
885686 ctx = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
0 commit comments