github.com/zmap/zlint@v1.1.0/lints/lint_name_constraint_empty.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   Restrictions are defined in terms of permitted or excluded name
    19     subtrees.  Any name matching a restriction in the excludedSubtrees
    20     field is invalid regardless of information appearing in the
    21     permittedSubtrees.  Conforming CAs MUST mark this extension as
    22     critical and SHOULD NOT impose name constraints on the x400Address,
    23     ediPartyName, or registeredID name forms.  Conforming CAs MUST NOT
    24     issue certificates where name constraints is an empty sequence.  That
    25     is, either the permittedSubtrees field or the excludedSubtrees MUST
    26     be present.
    27  ************************************************************************/
    28  
    29  import (
    30  	"encoding/asn1"
    31  
    32  	"github.com/zmap/zcrypto/x509"
    33  	"github.com/zmap/zlint/util"
    34  )
    35  
    36  type nameConstraintEmpty struct{}
    37  
    38  func (l *nameConstraintEmpty) Initialize() error {
    39  	return nil
    40  }
    41  
    42  func (l *nameConstraintEmpty) CheckApplies(c *x509.Certificate) bool {
    43  	if !(util.IsExtInCert(c, util.NameConstOID)) {
    44  		return false
    45  	}
    46  	nc := util.GetExtFromCert(c, util.NameConstOID)
    47  	var seq asn1.RawValue
    48  	rest, err := asn1.Unmarshal(nc.Value, &seq) //only one sequence, so rest should be empty
    49  	if err != nil || len(rest) != 0 || seq.Tag != 16 || seq.Class != 0 || !seq.IsCompound {
    50  		return false
    51  	}
    52  	return true
    53  }
    54  
    55  func (l *nameConstraintEmpty) Execute(c *x509.Certificate) *LintResult {
    56  	nc := util.GetExtFromCert(c, util.NameConstOID)
    57  	var seq asn1.RawValue
    58  	_, err := asn1.Unmarshal(nc.Value, &seq) //only one sequence, so rest should be empty
    59  	if err != nil {
    60  		return &LintResult{Status: Fatal}
    61  	}
    62  	if len(seq.Bytes) == 0 {
    63  		return &LintResult{Status: Error}
    64  	}
    65  
    66  	return &LintResult{Status: Pass}
    67  }
    68  
    69  func init() {
    70  	RegisterLint(&Lint{
    71  		Name:          "e_name_constraint_empty",
    72  		Description:   "Conforming CAs MUST NOT issue certificates where name constraints is an empty sequence. That is, either the permittedSubtree or excludedSubtree fields must be present",
    73  		Citation:      "RFC 5280: 4.2.1.10",
    74  		Source:        RFC5280,
    75  		EffectiveDate: util.RFC5280Date,
    76  		Lint:          &nameConstraintEmpty{},
    77  	})
    78  }