Debian Codegraph @

v3/lints/rfc/lint_rsa_allowed_ku_ee.go

raw symbols SCIP
1 /*
2 * ZLint Copyright 2024 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 rfc
16
17 import (
18 "fmt"
19 "sort"
20 "strings"
21
22 "github.com/zmap/zcrypto/x509"
23 "github.com/zmap/zlint/v3/lint"
24 "github.com/zmap/zlint/v3/util"
25 )
26
27 type rsaAllowedKUEe struct{}
28
29 /************************************************
30 RFC 3279: 2.3.1 RSA Keys
31 If the keyUsage extension is present in an end entity certificate
32 which conveys an RSA public key, any combination of the following
33 values MAY be present:
34
35 digitalSignature;
36 nonRepudiation;
37 keyEncipherment; and
38 dataEncipherment.
39 ************************************************/
40
41 func init() {
42 lint.RegisterCertificateLint(&lint.CertificateLint{
43 LintMetadata: lint.LintMetadata{
44 Name: "e_rsa_allowed_ku_ee",
45 Description: "Key usage values digitalSignature, nonRepudiation, keyEncipherment, and dataEncipherment may only be present in an end entity certificate with an RSA key",
46 Citation: "RFC 3279: 2.3.1",
47 Source: lint.RFC3279,
48 EffectiveDate: util.RFC3279Date,
49 },
50 Lint: NewRsaAllowedKUEe,
51 })
52 }
53
54 func NewRsaAllowedKUEe() lint.LintInterface {
55 return &rsaAllowedKUEe{}
56 }
57
58 func (l *rsaAllowedKUEe) CheckApplies(c *x509.Certificate) bool {
59 return c.PublicKeyAlgorithm == x509.RSA && util.HasKeyUsageOID(c) && util.IsSubscriberCert(c)
60 }
61
62 func (l *rsaAllowedKUEe) Execute(c *x509.Certificate) *lint.LintResult {
63
64 //KeyUsageDigitalSignature: allowed
65 //KeyUsageContentCommitment: allowed
66 //KeyUsageKeyEncipherment: allowed
67 //KeyUsageDataEncipherment: allowed
68 //KeyUsageKeyAgreement: not allowed
69 //KeyUsageCertSign: not allowed
70 //KeyUsageCRLSign: not allowed
71 //KeyUsageEncipherOnly: not allowed
72 //KeyUsageDecipherOnly: not allowed
73
74 var invalidKUs []string
75
76 disallowedKUs := [5]x509.KeyUsage{x509.KeyUsageKeyAgreement, x509.KeyUsageCertSign, x509.KeyUsageCRLSign, x509.KeyUsageEncipherOnly, x509.KeyUsageDecipherOnly}
77
78 for _, disallowedKU := range disallowedKUs {
79 if util.HasKeyUsage(c, disallowedKU) {
80 invalidKUs = append(invalidKUs, util.KeyUsageToString[disallowedKU])
81 }
82 }
83
84 if len(invalidKUs) > 0 {
85 // Sort the invalid KUs to allow consistent ordering of Details messages for unit testing
86 sort.Strings(invalidKUs)
87 return &lint.LintResult{
88 Status: lint.Error,
89 Details: fmt.Sprintf("Subscriber certificate with an RSA key contains invalid key usage(s): %s", strings.Join(invalidKUs, ", ")),
90 }
91 }
92
93 return &lint.LintResult{Status: lint.Pass}
94 }