| 1 |
package rfc |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
} |