@@ -11,7 +11,6 @@ import (
1111 "crypto/x509/pkix"
1212 "encoding/asn1"
1313 "encoding/hex"
14- "errors"
1514 "fmt"
1615 "math/big"
1716 "net"
@@ -92,11 +91,6 @@ func tlsalpn01SrvWithCert(t *testing.T, acmeCert *tls.Certificate, tlsVersion ui
9291 Certificates : []tls.Certificate {},
9392 ClientAuth : tls .NoClientCert ,
9493 GetCertificate : func (clientHello * tls.ClientHelloInfo ) (* tls.Certificate , error ) {
95- // This is a backstop test for RFC 8738, Section 6. Go's
96- // tls.hostnameInSNI already does the right thing.
97- if net .ParseIP (clientHello .ServerName ) != nil {
98- return nil , errors .New ("TLS client used a bare IP address for SNI" )
99- }
10094 return acmeCert , nil
10195 },
10296 NextProtos : []string {"http/1.1" , ACMETLS1Protocol },
@@ -865,3 +859,84 @@ func TestTLSALPN01BadIdentifier(t *testing.T) {
865859 prob := detailedError (err )
866860 test .AssertContains (t , prob .Error (), "Identifier type for TLS-ALPN-01 challenge was not DNS or IP" )
867861}
862+
863+ // TestTLSALPN01ServerName tests compliance with RFC 8737, Sec. 3 (step 3) & RFC
864+ // 8738, Sec. 6.
865+ func TestTLSALPN01ServerName (t * testing.T ) {
866+ testCases := []struct {
867+ Name string
868+ Ident identifier.ACMEIdentifier
869+ CertNames []string
870+ CertIPs []net.IP
871+ IPv6 bool
872+ want string
873+ }{
874+ {
875+ Name : "DNS name" ,
876+ Ident : identifier .NewDNS ("example.com" ),
877+ CertNames : []string {"example.com" },
878+ want : "example.com" ,
879+ },
880+ {
881+ // RFC 8738, Sec. 6.
882+ Name : "IPv4 address" ,
883+ Ident : identifier .NewIP (netip .MustParseAddr ("127.0.0.1" )),
884+ CertIPs : []net.IP {net .ParseIP ("127.0.0.1" )},
885+ want : "1.0.0.127.in-addr.arpa" ,
886+ },
887+ {
888+ // RFC 8738, Sec. 6.
889+ Name : "IPv6 address" ,
890+ Ident : identifier .NewIP (netip .MustParseAddr ("::1" )),
891+ CertIPs : []net.IP {net .ParseIP ("::1" )},
892+ IPv6 : true ,
893+ want : "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" ,
894+ },
895+ }
896+
897+ for _ , tc := range testCases {
898+ t .Run (tc .Name , func (t * testing.T ) {
899+ ctx , cancel := context .WithTimeout (context .Background (), time .Millisecond * 500 )
900+ defer cancel ()
901+
902+ tlsConfig := & tls.Config {
903+ Certificates : []tls.Certificate {},
904+ ClientAuth : tls .NoClientCert ,
905+ NextProtos : []string {"http/1.1" , ACMETLS1Protocol },
906+ GetCertificate : func (clientHello * tls.ClientHelloInfo ) (* tls.Certificate , error ) {
907+ got := clientHello .ServerName
908+ if got != tc .want {
909+ return nil , fmt .Errorf ("Got host %#v, but want %#v" , got , tc .want )
910+ }
911+ return testTLSCert (tc .CertNames , tc .CertIPs , []pkix.Extension {testACMEExt }), nil
912+ },
913+ }
914+
915+ hs := httptest .NewUnstartedServer (http .DefaultServeMux )
916+ hs .TLS = tlsConfig
917+ hs .Config .TLSNextProto = map [string ]func (* http.Server , * tls.Conn , http.Handler ){
918+ ACMETLS1Protocol : func (_ * http.Server , conn * tls.Conn , _ http.Handler ) {
919+ _ = conn .Close ()
920+ },
921+ }
922+ if tc .IPv6 {
923+ l , err := net .Listen ("tcp" , "[::1]:0" )
924+ if err != nil {
925+ panic (fmt .Sprintf ("httptest: failed to listen on a port: %v" , err ))
926+ }
927+ hs .Listener = l
928+ }
929+ hs .StartTLS ()
930+ defer hs .Close ()
931+
932+ va , _ := setup (hs , "" , nil , nil )
933+
934+ // The actual test happens in the tlsConfig.GetCertificate function,
935+ // which the validation will call and depend on for its success.
936+ _ , err := va .validateTLSALPN01 (ctx , tc .Ident , expectedKeyAuthorization )
937+ if err != nil {
938+ t .Errorf ("Validation failed: %v" , err )
939+ }
940+ })
941+ }
942+ }
0 commit comments