github.com/zmap/zlint@v1.1.0/lints/lint_qcstatem_qcpds_lang_case.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  	"fmt"
    20  	"github.com/zmap/zcrypto/x509"
    21  	"github.com/zmap/zlint/util"
    22  	"unicode"
    23  )
    24  
    25  type qcStatemQcPdsLangCase struct{}
    26  
    27  func (this *qcStatemQcPdsLangCase) getStatementOid() *asn1.ObjectIdentifier {
    28  	return &util.IdEtsiQcsQcEuPDS
    29  }
    30  
    31  func (l *qcStatemQcPdsLangCase) Initialize() error {
    32  	return nil
    33  }
    34  
    35  func (l *qcStatemQcPdsLangCase) CheckApplies(c *x509.Certificate) bool {
    36  	if !util.IsExtInCert(c, util.QcStateOid) {
    37  		return false
    38  	}
    39  	if util.ParseQcStatem(util.GetExtFromCert(c, util.QcStateOid).Value, *l.getStatementOid()).IsPresent() {
    40  		return true
    41  	}
    42  	return false
    43  }
    44  
    45  func isOnlyLowerCaseLetters(s string) bool {
    46  	for _, c := range s {
    47  		if !unicode.IsLower(c) {
    48  			return false
    49  		}
    50  	}
    51  	return true
    52  }
    53  
    54  func (l *qcStatemQcPdsLangCase) Execute(c *x509.Certificate) *LintResult {
    55  	errString := ""
    56  	wrnString := ""
    57  	ext := util.GetExtFromCert(c, util.QcStateOid)
    58  	s := util.ParseQcStatem(ext.Value, *l.getStatementOid())
    59  	errString += s.GetErrorInfo()
    60  	if len(errString) == 0 {
    61  		pds := s.(util.EtsiQcPds)
    62  		for i, loc := range pds.PdsLocations {
    63  			if !isOnlyLowerCaseLetters(loc.Language) {
    64  				util.AppendToStringSemicolonDelim(&wrnString, fmt.Sprintf("PDS location %d has a language code containing invalid letters", i))
    65  			}
    66  
    67  		}
    68  	}
    69  	if len(errString) == 0 {
    70  		if len(wrnString) == 0 {
    71  			return &LintResult{Status: Pass}
    72  		} else {
    73  			return &LintResult{Status: Warn, Details: wrnString}
    74  		}
    75  	} else {
    76  		return &LintResult{Status: Error, Details: errString}
    77  	}
    78  }
    79  
    80  func init() {
    81  	RegisterLint(&Lint{
    82  		Name:          "w_qcstatem_qcpds_lang_case",
    83  		Description:   "Checks that a QC Statement of the type id-etsi-qcs-QcPDS features a language code comprised of only lower case letters",
    84  		Citation:      "ETSI EN 319 412 - 5 V2.2.1 (2017 - 11) / Section 4.3.4",
    85  		Source:        EtsiEsi,
    86  		EffectiveDate: util.EtsiEn319_412_5_V2_2_1_Date,
    87  		Lint:          &qcStatemQcPdsLangCase{},
    88  	})
    89  }