@@ -25,6 +25,7 @@ import (
25
25
"crypto/x509/pkix"
26
26
"encoding/pem"
27
27
"fmt"
28
+ "math"
28
29
"math/big"
29
30
"net"
30
31
"os"
@@ -57,8 +58,14 @@ type AltNames struct {
57
58
// NewSelfSignedCACert creates a CA certificate
58
59
func NewSelfSignedCACert (cfg Config , key crypto.Signer ) (* x509.Certificate , error ) {
59
60
now := time .Now ()
61
+ // returns a uniform random value in [0, max-1), then add 1 to serial to make it a uniform random value in [1, max).
62
+ serial , err := cryptorand .Int (cryptorand .Reader , new (big.Int ).SetInt64 (math .MaxInt64 - 1 ))
63
+ if err != nil {
64
+ return nil , err
65
+ }
66
+ serial = new (big.Int ).Add (serial , big .NewInt (1 ))
60
67
tmpl := x509.Certificate {
61
- SerialNumber : new (big. Int ). SetInt64 ( 0 ) ,
68
+ SerialNumber : serial ,
62
69
Subject : pkix.Name {
63
70
CommonName : cfg .CommonName ,
64
71
Organization : cfg .Organization ,
@@ -116,9 +123,14 @@ func GenerateSelfSignedCertKeyWithFixtures(host string, alternateIPs []net.IP, a
116
123
if err != nil {
117
124
return nil , nil , err
118
125
}
119
-
126
+ // returns a uniform random value in [0, max-1), then add 1 to serial to make it a uniform random value in [1, max).
127
+ serial , err := cryptorand .Int (cryptorand .Reader , new (big.Int ).SetInt64 (math .MaxInt64 - 1 ))
128
+ if err != nil {
129
+ return nil , nil , err
130
+ }
131
+ serial = new (big.Int ).Add (serial , big .NewInt (1 ))
120
132
caTemplate := x509.Certificate {
121
- SerialNumber : big . NewInt ( 1 ) ,
133
+ SerialNumber : serial ,
122
134
Subject : pkix.Name {
123
135
CommonName : fmt .Sprintf ("%s-ca@%d" , host , time .Now ().Unix ()),
124
136
},
@@ -144,9 +156,14 @@ func GenerateSelfSignedCertKeyWithFixtures(host string, alternateIPs []net.IP, a
144
156
if err != nil {
145
157
return nil , nil , err
146
158
}
147
-
159
+ // returns a uniform random value in [0, max-1), then add 1 to serial to make it a uniform random value in [1, max).
160
+ serial , err = cryptorand .Int (cryptorand .Reader , new (big.Int ).SetInt64 (math .MaxInt64 - 1 ))
161
+ if err != nil {
162
+ return nil , nil , err
163
+ }
164
+ serial = new (big.Int ).Add (serial , big .NewInt (1 ))
148
165
template := x509.Certificate {
149
- SerialNumber : big . NewInt ( 2 ) ,
166
+ SerialNumber : serial ,
150
167
Subject : pkix.Name {
151
168
CommonName : fmt .Sprintf ("%s@%d" , host , time .Now ().Unix ()),
152
169
},
0 commit comments