github.com/zmap/zlint@v1.1.0/lints/lint_ca_is_ca.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  import (
    18  	"encoding/asn1"
    19  
    20  	"github.com/zmap/zcrypto/x509"
    21  	"github.com/zmap/zlint/util"
    22  )
    23  
    24  type caIsCA struct{}
    25  
    26  type basicConstraints struct {
    27  	IsCA       bool `asn1:"optional"`
    28  	MaxPathLen int  `asn1:"optional,default:-1"`
    29  }
    30  
    31  func (l *caIsCA) Initialize() error {
    32  	return nil
    33  }
    34  
    35  func (l *caIsCA) CheckApplies(c *x509.Certificate) bool {
    36  	return util.IsExtInCert(c, util.KeyUsageOID) && c.KeyUsage&x509.KeyUsageCertSign != 0 && util.IsExtInCert(c, util.BasicConstOID)
    37  }
    38  
    39  func (l *caIsCA) Execute(c *x509.Certificate) *LintResult {
    40  	e := util.GetExtFromCert(c, util.BasicConstOID)
    41  	var constraints basicConstraints
    42  	_, err := asn1.Unmarshal(e.Value, &constraints)
    43  	if err != nil {
    44  		return &LintResult{Status: Fatal}
    45  	}
    46  	if constraints.IsCA == true {
    47  		return &LintResult{Status: Pass}
    48  	} else {
    49  		return &LintResult{Status: Error}
    50  	}
    51  }
    52  
    53  func init() {
    54  	RegisterLint(&Lint{
    55  		Name:          "e_ca_is_ca",
    56  		Description:   "Root and Sub CA Certificate: The CA field MUST be set to true.",
    57  		Citation:      "BRs: 7.1.2.1, BRs: 7.1.2.2",
    58  		Source:        CABFBaselineRequirements,
    59  		EffectiveDate: util.CABEffectiveDate,
    60  		Lint:          &caIsCA{},
    61  	})
    62  }