github.com/zmap/zlint@v1.1.0/lints/lint_path_len_constraint_improperly_included.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  /******************************************************************
    18  RFC 5280: 4.2.1.9
    19  CAs MUST NOT include the pathLenConstraint field unless the cA
    20  boolean is asserted and the key usage extension asserts the
    21  keyCertSign bit.
    22  ******************************************************************/
    23  
    24  import (
    25  	"encoding/asn1"
    26  
    27  	"github.com/zmap/zcrypto/x509"
    28  	"github.com/zmap/zlint/util"
    29  )
    30  
    31  type pathLenIncluded struct{}
    32  
    33  func (l *pathLenIncluded) Initialize() error {
    34  	return nil
    35  }
    36  
    37  func (l *pathLenIncluded) CheckApplies(cert *x509.Certificate) bool {
    38  	return util.IsExtInCert(cert, util.BasicConstOID)
    39  }
    40  
    41  func (l *pathLenIncluded) Execute(cert *x509.Certificate) *LintResult {
    42  	bc := util.GetExtFromCert(cert, util.BasicConstOID)
    43  	var seq asn1.RawValue
    44  	var isCa bool
    45  	_, err := asn1.Unmarshal(bc.Value, &seq)
    46  	if err != nil {
    47  		return &LintResult{Status: Fatal}
    48  	}
    49  	if len(seq.Bytes) == 0 {
    50  		return &LintResult{Status: Pass}
    51  	}
    52  	rest, err := asn1.UnmarshalWithParams(seq.Bytes, &isCa, "optional")
    53  	if err != nil {
    54  		return &LintResult{Status: Fatal}
    55  	}
    56  	keyUsageValue := util.IsExtInCert(cert, util.KeyUsageOID)
    57  	if len(rest) > 0 && (!cert.IsCA || !keyUsageValue || (keyUsageValue && cert.KeyUsage&x509.KeyUsageCertSign == 0)) {
    58  		return &LintResult{Status: Error}
    59  	}
    60  	return &LintResult{Status: Pass}
    61  }
    62  
    63  func init() {
    64  	RegisterLint(&Lint{
    65  		Name:          "e_path_len_constraint_improperly_included",
    66  		Description:   "CAs MUST NOT include the pathLenConstraint field unless the CA boolean is asserted and the keyCertSign bit is set",
    67  		Citation:      "RFC 5280: 4.2.1.9",
    68  		Source:        RFC5280,
    69  		EffectiveDate: util.RFC3280Date,
    70  		Lint:          &pathLenIncluded{},
    71  	})
    72  }