Debian Codegraph @

v3/lints/rfc/lint_serial_number_longer_than_20_octets.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 "fmt"
19
20 "github.com/zmap/zcrypto/encoding/asn1"
21 "github.com/zmap/zcrypto/x509"
22 "github.com/zmap/zlint/v3/lint"
23 "github.com/zmap/zlint/v3/util"
24 )
25
26 type serialNumberTooLong struct{}
27
28 /************************************************
29 RFC 5280: 4.1.2.2. Serial Number
30 The serial number MUST be a positive integer assigned by the CA to each
31 certificate. It MUST be unique for each certificate issued by a given CA
32 (i.e., the issuer name and serial number identify a unique certificate).
33 CAs MUST force the serialNumber to be a non-negative integer.
34
35 Given the uniqueness requirements above, serial numbers can be expected to
36 contain long integers. Certificate users MUST be able to handle serialNumber
37 values up to 20 octets. Conforming CAs MUST NOT use serialNumber values longer
38 than 20 octets.
39
40 Note: Non-conforming CAs may issue certificates with serial numbers that are
41 negative or zero. Certificate users SHOULD be prepared togracefully handle
42 such certificates.
43 ************************************************/
44
45 func init() {
46 lint.RegisterCertificateLint(&lint.CertificateLint{
47 LintMetadata: lint.LintMetadata{
48 Name: "e_serial_number_longer_than_20_octets",
49 Description: "Certificates must not have a DER encoded serial number longer than 20 octets",
50 Citation: "RFC 5280: 4.1.2.2",
51 Source: lint.RFC5280,
52 EffectiveDate: util.RFC3280Date,
53 },
54 Lint: NewSerialNumberTooLong,
55 })
56 }
57
58 func NewSerialNumberTooLong() lint.LintInterface {
59 return &serialNumberTooLong{}
60 }
61
62 func (l *serialNumberTooLong) CheckApplies(c *x509.Certificate) bool {
63 return true
64 }
65
66 func (l *serialNumberTooLong) Execute(c *x509.Certificate) *lint.LintResult {
67 // Re-encode the certificate serial number and decode it back into
68 // an ASN1 raw value (which does little more than perform length computations,
69 // figures out the tag, etc.) so that we can easily see what the actual
70 // DER encoded lengths are without having to guess.
71 encoding, err := asn1.Marshal(c.SerialNumber)
72 if err != nil {
73 return &lint.LintResult{Status: lint.Fatal, Details: err.Error()}
74 }
75 serial := new(asn1.RawValue)
76 _, err = asn1.Unmarshal(encoding, serial)
77 if err != nil {
78 return &lint.LintResult{Status: lint.Fatal, Details: err.Error()}
79 }
80 length := len(serial.Bytes)
81 if length > 20 {
82 details := fmt.Sprintf("The DER encoded certificate serial number is %d octets long. "+
83 "If this is surprising to you, note that DER integers are signed and that SNs that are "+
84 "20 octets long with an MSB of 1 will be automatically prefixed with 0x00, thus bumping "+
85 "it up to 21 octets long. "+
86 "SN: %X", length, serial.Bytes)
87 return &lint.LintResult{Status: lint.Error, Details: details}
88 } else {
89 return &lint.LintResult{Status: lint.Pass}
90 }
91 }