| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package cabf_smime_br |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto/rsa" |
| 19 |
|
| 20 |
"github.com/zmap/zcrypto/x509" |
| 21 |
"github.com/zmap/zlint/v3/lint" |
| 22 |
"github.com/zmap/zlint/v3/util" |
| 23 |
) |
| 24 |
|
| 25 |
func init() { |
| 26 |
lint.RegisterCertificateLint(&lint.CertificateLint{ |
| 27 |
LintMetadata: lint.LintMetadata{ |
| 28 |
Name: "e_rsa_key_usage_strict", |
| 29 |
Description: "For signing only, bit positions SHALL be set for digitalSignature and MAY be set for nonRepudiation. For key management only, bit positions SHALL be set for keyEncipherment. For dual use, bit positions SHALL be set for digitalSignature and keyEncipherment and MAY be set for nonRepudiation.", |
| 30 |
Citation: "7.1.2.3.e", |
| 31 |
Source: lint.CABFSMIMEBaselineRequirements, |
| 32 |
EffectiveDate: util.CABF_SMIME_BRs_1_0_0_Date, |
| 33 |
}, |
| 34 |
Lint: NewRSAKeyUsageStrict, |
| 35 |
}) |
| 36 |
} |
| 37 |
|
| 38 |
type rsaKeyUsageStrict struct{} |
| 39 |
|
| 40 |
func NewRSAKeyUsageStrict() lint.LintInterface { |
| 41 |
return &rsaKeyUsageStrict{} |
| 42 |
} |
| 43 |
|
| 44 |
func (l *rsaKeyUsageStrict) CheckApplies(c *x509.Certificate) bool { |
| 45 |
if !(util.IsSubscriberCert(c) && util.IsStrictSMIMECertificate(c) && util.IsExtInCert(c, util.KeyUsageOID)) { |
| 46 |
return false |
| 47 |
} |
| 48 |
|
| 49 |
_, ok := c.PublicKey.(*rsa.PublicKey) |
| 50 |
return ok && c.PublicKeyAlgorithm == x509.RSA |
| 51 |
} |
| 52 |
|
| 53 |
func (l *rsaKeyUsageStrict) Execute(c *x509.Certificate) *lint.LintResult { |
| 54 |
const ( |
| 55 |
signing = iota + 1 |
| 56 |
keyManagement |
| 57 |
dualUsage |
| 58 |
) |
| 59 |
|
| 60 |
certType := 0 |
| 61 |
if util.HasKeyUsage(c, x509.KeyUsageDigitalSignature) { |
| 62 |
certType |= signing |
| 63 |
} |
| 64 |
if util.HasKeyUsage(c, x509.KeyUsageKeyEncipherment) { |
| 65 |
certType |= keyManagement |
| 66 |
} |
| 67 |
|
| 68 |
switch certType { |
| 69 |
case signing: |
| 70 |
mask := 0x1FF ^ (x509.KeyUsageDigitalSignature | x509.KeyUsageContentCommitment) |
| 71 |
if c.KeyUsage&mask != 0 { |
| 72 |
return &lint.LintResult{Status: lint.Error} |
| 73 |
} |
| 74 |
|
| 75 |
case keyManagement: |
| 76 |
mask := 0x1FF ^ (x509.KeyUsageKeyEncipherment) |
| 77 |
if c.KeyUsage&mask != 0 { |
| 78 |
return &lint.LintResult{Status: lint.Error} |
| 79 |
} |
| 80 |
|
| 81 |
case dualUsage: |
| 82 |
mask := 0x1FF ^ (x509.KeyUsageDigitalSignature | x509.KeyUsageContentCommitment | x509.KeyUsageKeyEncipherment) |
| 83 |
if c.KeyUsage&mask != 0 { |
| 84 |
return &lint.LintResult{Status: lint.Error} |
| 85 |
} |
| 86 |
|
| 87 |
default: |
| 88 |
return &lint.LintResult{Status: lint.NA} |
| 89 |
} |
| 90 |
|
| 91 |
return &lint.LintResult{Status: lint.Pass} |
| 92 |
} |