github.com/zmap/zlint@v1.1.0/lints/lint_idn_dnsname_must_be_nfc.go (about)

     1  package lints
     2  
     3  /*
     4   * ZLint Copyright 2018 Regents of the University of Michigan
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     7   * use this file except in compliance with the License. You may obtain a copy
     8   * of the License at http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    13   * implied. See the License for the specific language governing
    14   * permissions and limitations under the License.
    15   */
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/zmap/zcrypto/x509"
    21  	"github.com/zmap/zlint/util"
    22  	"golang.org/x/net/idna"
    23  	"golang.org/x/text/unicode/norm"
    24  )
    25  
    26  type IDNNotNFC struct{}
    27  
    28  func (l *IDNNotNFC) Initialize() error {
    29  	return nil
    30  }
    31  
    32  func (l *IDNNotNFC) CheckApplies(c *x509.Certificate) bool {
    33  	return util.IsExtInCert(c, util.SubjectAlternateNameOID)
    34  }
    35  
    36  func (l *IDNNotNFC) Execute(c *x509.Certificate) *LintResult {
    37  	for _, dns := range c.DNSNames {
    38  		labels := strings.Split(dns, ".")
    39  		for _, label := range labels {
    40  			if strings.HasPrefix(label, "xn--") {
    41  				unicodeLabel, err := idna.ToUnicode(label)
    42  				if err != nil {
    43  					return &LintResult{Status: NA}
    44  				}
    45  				if !norm.NFC.IsNormalString(unicodeLabel) {
    46  					return &LintResult{Status: Error}
    47  				}
    48  			}
    49  		}
    50  	}
    51  	return &LintResult{Status: Pass}
    52  }
    53  
    54  func init() {
    55  	RegisterLint(&Lint{
    56  		Name:          "e_international_dns_name_not_nfc",
    57  		Description:   "Internationalized DNSNames must be normalized by unicode normalization form C",
    58  		Citation:      "RFC 8399",
    59  		Source:        RFC5891,
    60  		EffectiveDate: util.RFC8399Date,
    61  		Lint:          &IDNNotNFC{},
    62  	})
    63  }