github.com/zmap/zlint@v1.1.0/lints/lint_qcstatem_qctype_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  	"fmt"
    20  	"github.com/zmap/zcrypto/x509"
    21  	"github.com/zmap/zlint/util"
    22  )
    23  
    24  type qcStatemQctypeValid struct{}
    25  
    26  func (this *qcStatemQctypeValid) getStatementOid() *asn1.ObjectIdentifier {
    27  	return &util.IdEtsiQcsQcType
    28  }
    29  
    30  func (l *qcStatemQctypeValid) Initialize() error {
    31  	return nil
    32  }
    33  
    34  func (l *qcStatemQctypeValid) CheckApplies(c *x509.Certificate) bool {
    35  	if !util.IsExtInCert(c, util.QcStateOid) {
    36  		return false
    37  	}
    38  	if util.ParseQcStatem(util.GetExtFromCert(c, util.QcStateOid).Value, *l.getStatementOid()).IsPresent() {
    39  		return true
    40  	}
    41  	return false
    42  }
    43  
    44  func (l *qcStatemQctypeValid) Execute(c *x509.Certificate) *LintResult {
    45  
    46  	errString := ""
    47  	ext := util.GetExtFromCert(c, util.QcStateOid)
    48  	s := util.ParseQcStatem(ext.Value, *l.getStatementOid())
    49  	errString += s.GetErrorInfo()
    50  	if len(errString) == 0 {
    51  		qcType := s.(util.Etsi423QcType)
    52  		if len(qcType.TypeOids) == 0 {
    53  			errString += "no QcType present, sequence of OIDs is empty"
    54  		}
    55  		for _, t := range qcType.TypeOids {
    56  
    57  			if !t.Equal(util.IdEtsiQcsQctEsign) && !t.Equal(util.IdEtsiQcsQctEseal) && !t.Equal(util.IdEtsiQcsQctWeb) {
    58  				if len(errString) > 0 {
    59  					errString += "; "
    60  				}
    61  				errString += fmt.Sprintf("encountered invalid ETSI QcType OID: %v", t)
    62  			}
    63  		}
    64  	}
    65  
    66  	if len(errString) == 0 {
    67  		return &LintResult{Status: Pass}
    68  	} else {
    69  		return &LintResult{Status: Error, Details: errString}
    70  	}
    71  }
    72  
    73  func init() {
    74  	RegisterLint(&Lint{
    75  		Name:          "e_qcstatem_qctype_valid",
    76  		Description:   "Checks that a QC Statement of the type Id-etsi-qcs-QcType features a non-empty list of only the allowed QcType OIDs",
    77  		Citation:      "ETSI EN 319 412 - 5 V2.2.1 (2017 - 11) / Section 4.2.3",
    78  		Source:        EtsiEsi,
    79  		EffectiveDate: util.EtsiEn319_412_5_V2_2_1_Date,
    80  		Lint:          &qcStatemQctypeValid{},
    81  	})
    82  }