github.com/zmap/zlint@v1.1.0/lints/lint_dnsname_bad_character_in_label.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  	"regexp"
    19  
    20  	"github.com/zmap/zcrypto/x509"
    21  	"github.com/zmap/zlint/util"
    22  )
    23  
    24  type DNSNameProperCharacters struct {
    25  	CompiledExpression *regexp.Regexp
    26  }
    27  
    28  func (l *DNSNameProperCharacters) Initialize() error {
    29  	const dnsNameRegexp = `^(\*\.)?(\?\.)*([A-Za-z0-9*_-]+\.)*[A-Za-z0-9*_-]*$`
    30  	var err error
    31  	l.CompiledExpression, err = regexp.Compile(dnsNameRegexp)
    32  
    33  	return err
    34  }
    35  
    36  func (l *DNSNameProperCharacters) CheckApplies(c *x509.Certificate) bool {
    37  	return util.IsSubscriberCert(c) && util.DNSNamesExist(c)
    38  }
    39  
    40  func (l *DNSNameProperCharacters) Execute(c *x509.Certificate) *LintResult {
    41  	if c.Subject.CommonName != "" && !util.CommonNameIsIP(c) {
    42  		if !l.CompiledExpression.MatchString(c.Subject.CommonName) {
    43  			return &LintResult{Status: Error}
    44  		}
    45  	}
    46  	for _, dns := range c.DNSNames {
    47  		if !l.CompiledExpression.MatchString(dns) {
    48  			return &LintResult{Status: Error}
    49  		}
    50  	}
    51  	return &LintResult{Status: Pass}
    52  }
    53  
    54  func init() {
    55  	RegisterLint(&Lint{
    56  		Name:          "e_dnsname_bad_character_in_label",
    57  		Description:   "Characters in labels of DNSNames MUST be alphanumeric, - , _ or *",
    58  		Citation:      "BRs: 7.1.4.2",
    59  		Source:        CABFBaselineRequirements,
    60  		EffectiveDate: util.CABEffectiveDate,
    61  		Lint:          &DNSNameProperCharacters{},
    62  	})
    63  }