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

     1  /*
     2   * ZLint Copyright 2017 Regents of the University of Michigan
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License. You may obtain a copy
     6   * of the License at http://www.apache.org/licenses/LICENSE-2.0
     7   *
     8   * Unless required by applicable law or agreed to in writing, software
     9   * distributed under the License is distributed on an "AS IS" BASIS,
    10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    11   * implied. See the License for the specific language governing
    12   * permissions and limitations under the License.
    13   */
    14  
    15  package lints
    16  
    17  import (
    18  	"encoding/asn1"
    19  	"unicode/utf8"
    20  
    21  	"github.com/zmap/zcrypto/x509"
    22  	"github.com/zmap/zlint/util"
    23  )
    24  
    25  type subjectDNNotPrintableCharacters struct{}
    26  
    27  func (l *subjectDNNotPrintableCharacters) Initialize() error {
    28  	return nil
    29  }
    30  
    31  func (l *subjectDNNotPrintableCharacters) CheckApplies(c *x509.Certificate) bool {
    32  	return true
    33  }
    34  
    35  func (l *subjectDNNotPrintableCharacters) Execute(c *x509.Certificate) *LintResult {
    36  	rdnSequence := util.RawRDNSequence{}
    37  	rest, err := asn1.Unmarshal(c.RawSubject, &rdnSequence)
    38  	if err != nil {
    39  		return &LintResult{Status: Fatal}
    40  	}
    41  	if len(rest) > 0 {
    42  		return &LintResult{Status: Fatal}
    43  	}
    44  
    45  	for _, attrTypeAndValueSet := range rdnSequence {
    46  		for _, attrTypeAndValue := range attrTypeAndValueSet {
    47  			bytes := attrTypeAndValue.Value.Bytes
    48  			for len(bytes) > 0 {
    49  				r, size := utf8.DecodeRune(bytes)
    50  				if r < 0x20 {
    51  					return &LintResult{Status: Error}
    52  				}
    53  				if r >= 0x7F && r <= 0x9F {
    54  					return &LintResult{Status: Error}
    55  				}
    56  				bytes = bytes[size:]
    57  			}
    58  		}
    59  	}
    60  
    61  	return &LintResult{Status: Pass}
    62  }
    63  
    64  func init() {
    65  	RegisterLint(&Lint{
    66  		Name:          "e_subject_dn_not_printable_characters",
    67  		Description:   "X520 Subject fields MUST only contain printable control characters",
    68  		Citation:      "RFC 5280: Appendix A",
    69  		Source:        RFC5280,
    70  		EffectiveDate: util.ZeroDate,
    71  		Lint:          &subjectDNNotPrintableCharacters{},
    72  	})
    73  }