github.com/zmap/zlint@v1.1.0/lints/lint_subject_contains_noninformational_value.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  /**********************************************************************************************************************
    18  BRs: 7.1.4.2.2
    19  Other Subject Attributes
    20  With the exception of the subject:organizationalUnitName (OU) attribute, optional attributes, when present within
    21  the subject field, MUST contain information that has been verified by the CA. Metadata such as ‘.’, ‘-‘, and ‘ ‘ (i.e.
    22  space) characters, and/or any other indication that the value is absent, incomplete, or not applicable, SHALL NOT
    23  be used.
    24  **********************************************************************************************************************/
    25  
    26  import (
    27  	"fmt"
    28  
    29  	"github.com/zmap/zcrypto/x509"
    30  	"github.com/zmap/zlint/util"
    31  )
    32  
    33  type illegalChar struct{}
    34  
    35  func (l *illegalChar) Initialize() error {
    36  	return nil
    37  }
    38  
    39  func (l *illegalChar) CheckApplies(c *x509.Certificate) bool {
    40  	return true
    41  }
    42  
    43  func (l *illegalChar) Execute(c *x509.Certificate) *LintResult {
    44  	for _, j := range c.Subject.Names {
    45  		value, ok := j.Value.(string)
    46  		if !ok {
    47  			continue
    48  		}
    49  
    50  		if !checkAlphaNumericOrUTF8Present(value) {
    51  			return &LintResult{Status: Error, Details: fmt.Sprintf("found only metadata %s in subjectDN attribute %s", value, j.Type.String())}
    52  		}
    53  	}
    54  
    55  	return &LintResult{Status: Pass}
    56  }
    57  
    58  func init() {
    59  	RegisterLint(&Lint{
    60  		Name:          "e_subject_contains_noninformational_value",
    61  		Description:   "Subject name fields must not contain '.','-',' ' or any other indication that the field has been omitted",
    62  		Citation:      "BRs: 7.1.4.2.2",
    63  		Source:        CABFBaselineRequirements,
    64  		EffectiveDate: util.CABEffectiveDate,
    65  		Lint:          &illegalChar{},
    66  	})
    67  }
    68  
    69  // checkAlphaNumericOrUTF8Present checks if input string contains at least one occurrence of [a-Z0-9] or
    70  // a UTF8 rune outside of ascii table
    71  func checkAlphaNumericOrUTF8Present(input string) bool {
    72  	for _, r := range input {
    73  		if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r > 127 {
    74  			return true
    75  		}
    76  	}
    77  
    78  	return false
    79  }