@@ -16,13 +16,13 @@ import (
1616)
1717
1818const (
19- defaultCAValidity = 10 * 365 * 24 * time .Hour
20- defaultCertificateTTL = 8 * time .Hour
21- defaultClockSkew = 5 * time .Minute
22- permOwnerReadWrite = 0o600
23- permOwnerReadWriteExec = 0o750
24- permOwnerReadGroupRead = 0o640
25- maxSerialShift = 128
19+ defaultCAValidity = 10 * 365 * 24 * time .Hour
20+ defaultCertificateTTL = 8 * time .Hour
21+ defaultClockSkew = 5 * time .Minute
22+ permOwnerReadWrite = 0o600
23+ permOwnerReadExecute = 0o750
24+ permOwnerReadGroup = 0o640
25+ maxSerialShift = 128
2626)
2727
2828type CertificateAuthority struct {
@@ -33,10 +33,10 @@ type CertificateAuthority struct {
3333}
3434
3535func LoadOrCreateCA (certPath , keyPath , commonName string , validFor time.Duration ) (* CertificateAuthority , error ) {
36- if err := os .MkdirAll (filepath .Dir (certPath ), permOwnerReadWriteExec ); err != nil {
36+ if err := os .MkdirAll (filepath .Dir (certPath ), permOwnerReadExecute ); err != nil {
3737 return nil , err
3838 }
39- if err := os .MkdirAll (filepath .Dir (keyPath ), permOwnerReadWriteExec ); err != nil {
39+ if err := os .MkdirAll (filepath .Dir (keyPath ), permOwnerReadExecute ); err != nil {
4040 return nil , err
4141 }
4242
@@ -78,7 +78,7 @@ func LoadOrCreateCA(certPath, keyPath, commonName string, validFor time.Duration
7878 return nil , err
7979 }
8080
81- if err := writeFileSecure (certPath , 0o640 , func (f * os.File ) error {
81+ if err := writeFileSecure (certPath , permOwnerReadGroup , func (f * os.File ) error {
8282 return pem .Encode (f , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
8383 }); err != nil {
8484 return nil , err
@@ -88,7 +88,7 @@ func LoadOrCreateCA(certPath, keyPath, commonName string, validFor time.Duration
8888 if err != nil {
8989 return nil , err
9090 }
91- if err := writeFileSecure (keyPath , 0o600 , func (f * os.File ) error {
91+ if err := writeFileSecure (keyPath , permOwnerReadWrite , func (f * os.File ) error {
9292 return pem .Encode (f , & pem.Block {Type : "PRIVATE KEY" , Bytes : encoded })
9393 }); err != nil {
9494 return nil , err
@@ -135,7 +135,7 @@ func LoadCA(certPath, keyPath string) (*CertificateAuthority, error) {
135135}
136136
137137func writeFileSecure (path string , perm os.FileMode , writeFn func (* os.File ) error ) error {
138- if err := os .MkdirAll (filepath .Dir (path ), dirPermPrivate ); err != nil {
138+ if err := os .MkdirAll (filepath .Dir (path ), permOwnerReadExecute ); err != nil {
139139 return err
140140 }
141141
@@ -153,7 +153,7 @@ func (c *CertificateAuthority) IssueCertificate(subject pkix.Name, uris []string
153153 ttl = defaultCertificateTTL
154154 }
155155
156- serial , err := rand .Int (rand .Reader , maxSerialNumber )
156+ serial , err := rand .Int (rand .Reader , new (big. Int ). Lsh ( big . NewInt ( 1 ), maxSerialShift ) )
157157 if err != nil {
158158 return nil , err
159159 }
@@ -199,7 +199,7 @@ func (c *CertificateAuthority) SignCSR(csr *x509.CertificateRequest, ttl time.Du
199199 if ttl == 0 {
200200 ttl = defaultCertificateTTL
201201 }
202- serial , err := rand .Int (rand .Reader , maxSerialNumber )
202+ serial , err := rand .Int (rand .Reader , new (big. Int ). Lsh ( big . NewInt ( 1 ), maxSerialShift ) )
203203 if err != nil {
204204 return nil , err
205205 }
0 commit comments