Debian Codegraph @

v3/lints/rfc/lint_path_len_constraint_improperly_included.go

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