@@ -91,7 +91,14 @@ function checkServerIdentity(host, cert) {
9191 // The same applies to hostname with more than one wildcard,
9292 // if hostname has wildcard when wildcards are not allowed,
9393 // or if there are less than two dots after wildcard (i.e. *.com or *d.com)
94- if ( / \* .* \* / . test ( host ) || ! wildcards && / \* / . test ( host ) ||
94+ //
95+ // also
96+ //
97+ // "The client SHOULD NOT attempt to match a presented identifier in
98+ // which the wildcard character comprises a label other than the
99+ // left-most label (e.g., do not match bar.*.example.net)."
100+ // RFC6125
101+ if ( ! wildcards && / \* / . test ( host ) || / [ \. \* ] .* \* / . test ( host ) ||
95102 / \* / . test ( host ) && ! / \* .* \. .+ \. .+ / . test ( host ) ) {
96103 return / $ ./ ;
97104 }
@@ -112,6 +119,7 @@ function checkServerIdentity(host, cert) {
112119 var dnsNames = [ ] ,
113120 uriNames = [ ] ,
114121 ips = [ ] ,
122+ matchCN = true ,
115123 valid = false ;
116124
117125 // There're several names to perform check against:
@@ -120,6 +128,7 @@ function checkServerIdentity(host, cert) {
120128 //
121129 // Walk through altnames and generate lists of those names
122130 if ( cert . subjectaltname ) {
131+ matchCN = false ;
123132 cert . subjectaltname . split ( / , / g) . forEach ( function ( altname ) {
124133 if ( / ^ D N S : / . test ( altname ) ) {
125134 dnsNames . push ( altname . slice ( 4 ) ) ;
@@ -155,14 +164,24 @@ function checkServerIdentity(host, cert) {
155164
156165 dnsNames = dnsNames . concat ( uriNames ) ;
157166
158- // And only after check if hostname matches CN
159- var commonNames = cert . subject . CN ;
160- if ( Array . isArray ( commonNames ) ) {
161- for ( var i = 0 , k = commonNames . length ; i < k ; ++ i ) {
162- dnsNames . push ( regexpify ( commonNames [ i ] , true ) ) ;
167+ if ( dnsNames . length > 0 ) matchCN = false ;
168+
169+ // Match against Common Name (CN) only if altnames are not present.
170+ //
171+ // "As noted, a client MUST NOT seek a match for a reference identifier
172+ // of CN-ID if the presented identifiers include a DNS-ID, SRV-ID,
173+ // URI-ID, or any application-specific identifier types supported by the
174+ // client."
175+ // RFC6125
176+ if ( matchCN ) {
177+ var commonNames = cert . subject . CN ;
178+ if ( Array . isArray ( commonNames ) ) {
179+ for ( var i = 0 , k = commonNames . length ; i < k ; ++ i ) {
180+ dnsNames . push ( regexpify ( commonNames [ i ] , true ) ) ;
181+ }
182+ } else {
183+ dnsNames . push ( regexpify ( commonNames , true ) ) ;
163184 }
164- } else {
165- dnsNames . push ( regexpify ( commonNames , true ) ) ;
166185 }
167186
168187 valid = dnsNames . some ( function ( re ) {
0 commit comments