github.com/zmap/zlint@v1.1.0/lints/lint_qcstatem_qclimitvalue_valid.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"
    20  
    21  	"github.com/zmap/zcrypto/x509"
    22  	"github.com/zmap/zlint/util"
    23  )
    24  
    25  type qcStatemQcLimitValueValid struct{}
    26  
    27  func (this *qcStatemQcLimitValueValid) getStatementOid() *asn1.ObjectIdentifier {
    28  	return &util.IdEtsiQcsQcLimitValue
    29  }
    30  
    31  func (l *qcStatemQcLimitValueValid) Initialize() error {
    32  	return nil
    33  }
    34  
    35  func (l *qcStatemQcLimitValueValid) 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 isOnlyLetters(s string) bool {
    46  	for _, r := range s {
    47  		if !unicode.IsLetter(r) {
    48  			return false
    49  		}
    50  	}
    51  	return true
    52  }
    53  
    54  func (l *qcStatemQcLimitValueValid) Execute(c *x509.Certificate) *LintResult {
    55  
    56  	errString := ""
    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  		qcLv, ok := s.(util.EtsiQcLimitValue)
    62  		if !ok {
    63  			return &LintResult{Status: Error, Details: "parsed QcStatem is not a EtsiQcLimitValue"}
    64  		}
    65  		if qcLv.Amount < 0 {
    66  			util.AppendToStringSemicolonDelim(&errString, "amount is negative")
    67  		}
    68  		if qcLv.IsNum {
    69  			if qcLv.CurrencyNum < 1 || qcLv.CurrencyNum > 999 {
    70  				util.AppendToStringSemicolonDelim(&errString, "numeric currency code is out of range")
    71  			}
    72  		} else {
    73  			if len(qcLv.CurrencyAlph) != 3 {
    74  				util.AppendToStringSemicolonDelim(&errString, "invalid string length of currency code")
    75  			}
    76  			if !isOnlyLetters(qcLv.CurrencyAlph) {
    77  				util.AppendToStringSemicolonDelim(&errString, "currency code string contains not only letters")
    78  			}
    79  
    80  		}
    81  
    82  	}
    83  	if len(errString) == 0 {
    84  		return &LintResult{Status: Pass}
    85  	} else {
    86  		return &LintResult{Status: Error, Details: errString}
    87  	}
    88  }
    89  
    90  func init() {
    91  	RegisterLint(&Lint{
    92  		Name:          "e_qcstatem_qclimitvalue_valid",
    93  		Description:   "Checks that a QC Statement of the type id-etsi-qcs-QcLimitValue has the correct form",
    94  		Citation:      "ETSI EN 319 412 - 5 V2.2.1 (2017 - 11) / Section 4.3.2",
    95  		Source:        EtsiEsi,
    96  		EffectiveDate: util.EtsiEn319_412_5_V2_2_1_Date,
    97  		Lint:          &qcStatemQcLimitValueValid{},
    98  	})
    99  }