Skip to content

Commit d9c73bb

Browse files
Merge pull request #726 from marun/ca-random-serial
Bug 1810036: Set a random serial number for signing certificate templates
2 parents 81598ff + a6f04ac commit d9c73bb

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

pkg/crypto/crypto.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,16 @@ type RandomSerialGenerator struct {
526526
}
527527

528528
func (s *RandomSerialGenerator) Next(template *x509.Certificate) (int64, error) {
529+
return randomSerialNumber(), nil
530+
}
531+
532+
// randomSerialNumber returns a random int64 serial number based on
533+
// time.Now. It is defined separately from the generator interface so
534+
// that the caller doesn't have to worry about an input template or
535+
// error - these are unnecessary when creating a random serial.
536+
func randomSerialNumber() int64 {
529537
r := mathrand.New(mathrand.NewSource(time.Now().UTC().UnixNano()))
530-
return r.Int63(), nil
538+
return r.Int63()
531539
}
532540

533541
// EnsureCA returns a CA, whether it was created (as opposed to pre-existing), and any error
@@ -908,9 +916,13 @@ func newSigningCertificateTemplateForDuration(subject pkix.Name, caLifetime time
908916

909917
SignatureAlgorithm: x509.SHA256WithRSA,
910918

911-
NotBefore: currentTime().Add(-1 * time.Second),
912-
NotAfter: currentTime().Add(caLifetime),
913-
SerialNumber: big.NewInt(1),
919+
NotBefore: currentTime().Add(-1 * time.Second),
920+
NotAfter: currentTime().Add(caLifetime),
921+
922+
// Specify a random serial number to avoid the same issuer+serial
923+
// number referring to different certs in a chain of trust if the
924+
// signing certificate is ever rotated.
925+
SerialNumber: big.NewInt(randomSerialNumber()),
914926

915927
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
916928
BasicConstraintsValid: true,

0 commit comments

Comments
 (0)